Seinfeld

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1368    Accepted Submission(s): 674
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
 
Source

题意:给定一个括号序列,求最少经过多少操作能使其变为合法序列。操作仅仅能是把左括号变为右括号或者右括号变为左括号。

题解:每次读取一个字符,若是左括号。那么left++。若是右括号,那么left--,若left==0,那么须要一次操作以将右括号变为左括号。

#include <stdio.h>
#include <string.h> #define maxn 2010 char str[maxn]; int main() {
int i, left, op, cas = 1;
while(scanf("%s", str), str[0] != '-') {
left = op = 0;
for(i = 0; str[i]; ++i) {
if(str[i] == '{') ++left;
else if(0 == left) {
++left; ++op;
} else --left;
}
op += left / 2;
printf("%d. %d\n", cas++, op);
}
return 0;
}

HDU3351 Seinfeld 【贪心】的更多相关文章

  1. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  7. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  8. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  9. 【贪心】HDU 1257

    HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...

随机推荐

  1. POJ 2109 Inner Vertices(扫描线+树状数组)

    [题目链接] http://poj.org/problem?id=3109 [题目大意] 在一个棋盘上放满白子,现在把一些白子变成黑子, 如果一个白子上下左右都有黑子,就会变成黑子,问最终黑子个数 [ ...

  2. 【译】PHP之道(PHP the right way)

    刚入门的同学觉得自己能用PHP写出各种程序就很NB了,但其实作为一个专业程序员,你得有个全面点的知识结构.单元测试总得听过,文档工具总得用用,Xss总得会防.推荐大家把<PHP the righ ...

  3. intel 硬盘加速技术

    Intel Smart Response Technology 混合硬盘技术 Intel Rapid Storage Technology SERVER:

  4. mtd-utils 及 ubi-utils 交叉编译

    参考: http://blog.csdn.net/zjjyliuweijie/article/details/7205374 NAND是嵌入式系统一个很重要的部件,而mtd-utilts中包含了很多针 ...

  5. DATASNAP数据序列之FIREDAC的TFDJSONDataSets

    DATASNAP数据序列之FIREDAC的TFDJSONDataSets DELPHI XE5开始增加了新的数据引擎——FIREDAC,它是跨平台的数据引擎,WINDOWS.LINUX.MAC.APP ...

  6. Android服务Service具体解释(作用,生命周期,AIDL)系列文章-为什么须要服务呢?

    Android服务Service具体解释(作用,生命周期,AIDL) 近期沉迷于上班,没有时间写博客了.解衣入睡,未眠.随起床写一篇博客压压惊! ##我们android系统为什么须要服务Service ...

  7. 【MySQL】谈谈PhxSQL的设计和实现哲学

    参考资料: http://mp.weixin.qq.com/s?__biz=MzI4NDMyNTU2Mw==&mid=2247483790&idx=1&sn=c925202df ...

  8. 在windows上一键编译各种版本的protobuf(2017-12-05修改)

    所需工具 : cmake  for  windows 和  git for windows 原理:protobuf 是google的一个开源项目,其源代码在github上可以下载到,并且源码都采用cm ...

  9. es6 - filter for-chrome

    'use strict'; let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; // 除去取余2的 - es6 let es5OddNumbers = numbers ...

  10. S3:代理模式 Proxy

    为其他对象提供一种代理以控制对这个对象的访问. 使用场合: 1.远程代理:为一个对象在不同的地址空间提供局部代表,隐藏对象存在于不同地址空间的事实.2.虚拟代理:根据需要创建开销很大的对象,通过它来存 ...