Seinfeld

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1382    Accepted Submission(s): 683

Problem Description
I’m out of stories. For years I’ve been writing stories, some rather silly, just to make simple problems look difficult and complex problems look easy. But, alas, not for this one.
You’re given a non empty string made in its entirety from opening and closing braces. Your task is to find the minimum number of “operations” needed to make the string stable. The definition for being stable is as follows:
1. An empty string is stable.
2. If S is stable, then {S} is also stable.
3. If S and T are both stable, then ST (the concatenation of the two) is also stable.
All of these strings are stable: {}, {}{}, and {{}{}}; But none of these: }{, {{}{, nor {}{.
The only operation allowed on the string is to replace an opening brace with a closing brace, or visa-versa.
Input
Your program will be tested on one or more data sets. Each data set is described on a single line. The line is a non-empty string of opening and closing braces and nothing else. No string has more than 2000 braces. All sequences are of even length.
The last line of the input is made of one or more ’-’ (minus signs.)
Output
For each test case, print the following line:
k. N
Where k is the test case number (starting at one,) and N is the minimum number of operations needed to convert the given string into a balanced one.
Note: There is a blank space before N.
Sample Input
}{ {}{}{} {{{} ---
Sample Output
1. 2 2. 0 3. 1
#include<stdio.h>
#include<string.h>
int main()
{int k=1;int a,t,i,len;char str[2010];while(gets(str)&&str[0]!='-'){int a=t=0;len=strlen(str);for(i=0;i<len;i++){if(str[i]=='{'){t++;}else{if(t){t--;}else{t++;a++;}}}a=a+t/2;printf("%d. %d\n",k,a);k++;}return 0;
}

转载于:https://www.cnblogs.com/gccbuaa/p/7232180.html

Seinfeld(杭电3351)相关推荐

  1. hdoj杭电问题分类

    杭电上的题虽然多,但是一直苦于找不到问题分类,网页都是英文的,所以平时做题也没怎么看,今天仔细一看,问题分类竟然就在主页....做了那么久的题居然没发现,表示已经狗带..不要笑,不知道有没有像我一样傻 ...

  2. 【杭电ACM】1097 A hard puzzle

    [杭电ACM]1097  A hard puzzle http://acm.hdu.edu.cn/showproblem.php?pid=1097 先用int手写了算法结果竟然wrong answer ...

  3. 【ACM】杭电OJ 1106 函数atoi

    函数atoi是把字符串转化成整数的函数,头文件为 #include "stdlib.h" e.g. 运行环境:Dev-C++ 5.11 杭电1106 调用了sort函数,运行的时间 ...

  4. 【ACM】杭电OJ 2037

    题目链接:杭电OJ 2037 先把b[i]进行排序,然后,b[i]与a[i+1]进行比较. #include <iostream> #include <cstdio> #inc ...

  5. 【ACM】杭电OJ 2020(排序)

    题目链接:杭电OJ 2020 排序可以有冒泡排序,选择排序,或者直接调用函数. 下面是选择排序: #include <stdio.h> #include <math.h> in ...

  6. 【ACM】杭电OJ 2018

    题目链接:杭电OJ 2018 从n>4开始,每一年的牛的数量=前一年的牛的数量+三年前的牛的数量 问:为什么是三年前? 答:假设三年前有一头小牛出生,出生的那一年即为第一年,到了第四年,即三年后 ...

  7. 【ACM】杭电OJ 1005

     题目链接:杭电OJ 1005 超时代码如下(而且开辟的数组空间大小不够): #include <stdio.h> int m[100000]; int f(int n,int a,int ...

  8. 【ACM】杭电OJ 1004

     题目链接:杭电OJ 1004 运行环境:Dev-C++ 5.11 思路: 先把先把num数组全部赋值为1:第一个颜色单独输入,从第二个开始,需要与前面的进行比较,如果前面有相同的颜色,则在目前的nu ...

  9. 【ACM】杭电OJ 2012。

    题目链接:杭电OJ 2012 思路很简单,但是有一种高效算法显示编译错误,不知道为什么 运行环境:VS2017 AC代码: #include <stdio.h> #include < ...

  10. 【ACM】杭电OJ 1003。

    运行环境VS2017  题目链接:杭电OJ 1003 主要思想是: 用d[i]来存放前i项中最大的和,得到end,然后再倒推,得起始的位置begin 然而在程序42行的疑问,大家可以讨论一下吗???? ...

最新文章

  1. 有存款,才能过得更踏实
  2. 关于学习Python的一点学习总结(54->集合->堆->双端队列)
  3. c语言printf输出格式顶格,c语言printf()输出格式[共6页].pdf
  4. 【简洁写法】剑指 Offer 30. 包含min函数的栈
  5. J2EE搭建Dynamic web SpringMVC工程404错误分析(二)
  6. rmi远程代码执行漏洞_fastjson远程代码执行漏洞复现
  7. SQL Server 和 Oracle 以及 MySQL 有哪些区别?
  8. 给公司部门设计的SOA架构
  9. (转)编码剖析@Resource注解的实现原理
  10. Linux查看某个进程的启动时间
  11. 【Git】GitHub for Windows使用(1)
  12. PyQt5快速入门教程4-QtDesigner设计登录界面
  13. 供应链金融的产品业务系统应用架构设计
  14. 范德堡大学计算机专业课程,2020年范德堡大学专业设置
  15. Android 蓝牙协议栈消息(bta_sys_sendmsg)发送机制 ---- 全网唯一解析
  16. Make sure that `gem install libv8 -v '3.16.14.3'` succeeds before bundling.
  17. spider存储引擎全面讲解
  18. 整理了百大框架排行榜
  19. 多线程:synchronized关键字解析
  20. channel练习题

热门文章

  1. [渝粤教育] 西南科技大学 农业技术经济学 在线考试复习资料
  2. 【渝粤教育】电大中专电子商务网站建设与维护_1作业 题库
  3. 【渝粤教育】广东开放大学 学前儿童保育学 形成性考核 (40)
  4. matlab中的小函数
  5. hdu 2513 cake clicing(切蛋糕)
  6. C# 中的字符串内插
  7. Codeforces Round #484 (Div. 2) D. Shark
  8. 2016012013 王雪 散列函数的应用及其安全性
  9. 输出匹配项:grep
  10. OleDb与Access数据访问中日期时间参数类型错误和命名参数的用法