杭电 Seinfeld
来源:互联网 发布:www.2987js.com 编辑:程序博客网 时间:2024/05/21 06:23
Total Submission(s): 1354 Accepted Submission(s): 665
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.
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.)
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.
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. 22. 03. 1#include<iostream>#include<string>using namespace std;int main(){int i,j,k,l,t=1;string s;while(cin>>s&&s[0]!='-'){j=0;k=0;l=s.length();for(i=0;i<l;i++){if(s[i]=='{')j++;else{if(j!=0){j--;}else{k++;j++;}}}k=k+j/2;printf("%d. %d\n",t,k);t++;}return 0;}
0 0
- 杭电 Seinfeld
- Seinfeld(杭电3351)
- Seinfeld杭电3351
- 杭电ACM HDU 3351 Seinfeld
- 杭电3351 Seinfeld(贪心)
- Seinfeld
- Seinfeld
- Seinfeld
- Seinfeld
- HDU_3351 Seinfeld
- HDU3351 -Seinfeld
- POJ 3991 Seinfeld
- HDU3351 Seinfeld 【贪心】
- hdu-oj 3351 Seinfeld
- 1410282310-hd-Seinfeld
- HDU 3351 Seinfeld
- POJ 3991 Seinfeld
- POJ 3991 Seinfeld
- hdu 4961 Boring Sum
- Meet Python
- 用串口连接GSM手机发送和接收短消息,在应用程序中如何编程实现?
- Android博文索引
- 谷歌2015RoundA第一题
- 杭电 Seinfeld
- poj1128 dfs好题 voj1030
- 图片轮播_支持显示网络图片及下载图片至SD后再显示
- android 技术大牛 BLOG
- Android帧动画的实现
- [九度 1510 剑指offer]—替换空格 数组插入逆向移动
- 【技巧】JAVA 日期问题
- 常用运算符和函数、case\top\over
- AppCode3真的很不错