Seinfeld

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

Total Submission(s): 1373    Accepted Submission(s): 678
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>
#include <stack>
#include <algorithm>
using namespace std; char c[1005], temp[1005];
stack<char> s;
int main(){
int v = 1;
while(gets(c), c[0] != '-'){
int i, len = strlen(c);
s.push(c[0]);
for(i = 1; i < len; i ++){
if(!s.empty()){
if(s.top() == '{'&&c[i] == '}'){
s.pop(); continue;
}
else s.push(c[i]);
}
else s.push(c[i]);
}
int sum1 = 0, sum2 = 0;
while(!s.empty()){
if(s.top() == '}') ++sum1;
else ++sum2;
s.pop();
}
int ans = (sum1+1)/2+(sum2+1)/2;
printf("%d. %d\n", v++, ans);
}
return 0;
}

hdoj 3351 Seinfeld 【栈的简单应用】的更多相关文章

  1. hdu Train Problem I(栈的简单应用)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  2. 栈回溯简单实现(x86)

    0x01  栈简介  首先局部变量的分配释放是通过调整栈指针实现的,栈为函数调用和定义局部变量提供了一块简单易用的空间,定义在栈上的变量不必考虑内存申请和释放.只要调整栈指针就可以分配和释放内存.   ...

  3. Javascript的堆和栈的简单理解

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 栈及其简单应用(二)(python代码)

    一.括号判定 前一篇文章我们介绍了栈的简单应用中,关于括号的判定,但那只是一种括号的判定,下面我们来介绍多种括号混合使用时,如何判断括号左右一一对应. 比如“{}{(}(][”这种情况,需要对一种括号 ...

  5. 栈及其简单应用(python代码)

    栈属于线性结构(Linear Struncture),要搞清楚这个概念,首先要明白”栈“原来的意思,如此才能把握本质."栈“者,存储货物或供旅客住宿的地方,可引申为仓库.中转站,所以引入到计 ...

  6. HDU 3351 Seinfeld(括号匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3351 解题报告:输入一个只有'{'跟'}'的字符串,有两种操作,一种是把'{'变成'}',另一种是'} ...

  7. HDOJ 1022 模拟栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)

    栈的练习,如此水题竟然做了两个小时... 题意:给出矩阵大小和矩阵的运算顺序,判断能否相乘并求运算量. 我的算法很简单:比如(((((DE)F)G)H)I),遇到 (就cnt累计加一,字母入栈,遇到) ...

  9. HDU 3351 Seinfeld 宋飞正传(水)

    题意: 给出一个串,串内只有大括号,问经过几次改变可使全部括号合法?改变指的是可以将某一方向的括号变成另一方向. 思路: 利用栈的特点,若出现成对的合法括号,直接删掉,留下那些不合法的成为一串.既然不 ...

随机推荐

  1. zabbix设置发送消息的时间

    需求:比如我有两个报警的媒介:邮件和微信,但是下班之后,晚上我不希望手机一直响,打扰我睡觉,邮件无所谓,可以24h发送 分析:那现在就需要把微信分时间段发送:比如06:00-24:00   这个时间点 ...

  2. 服务器禁用ping

    linux禁ping.这里操作的是centos6.5内核参数禁ping禁用ping #echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all启用ping ...

  3. LayuI固定块关闭

    1.近期项目使用了layui的固定块,但是当到某个独立页面时,固定块还在,就显得突兀: 2.通过F12查看,发现代码: <ul class="layui-fixbar" st ...

  4. sublime中使用markdown并实时编辑

    1.需求 想在sublime中编辑.md文件 2.步骤 找到菜单栏: 快捷键,shift + command + p,选择 Package Control:Install Package, 没有找到P ...

  5. js检测对象是否是数组

    js检测对象是否是数组 可以通过instanceof 方法一. var arr=[] arr instanceof Array   //true var obj={} obj instanceof A ...

  6. 18. PROFILING

    18. PROFILING PROFILING表提供语句分析信息. 其内容对应于SHOW PROFILE和SHOW PROFILES语句生成的信息(参见"SHOW PROFILE语法&quo ...

  7. XV6锁

    锁 xv6 运行在多处理器上,即计算机上有多个单独执行代码的 CPU.这些 CPU 操作同一片地址空间并分享其中的数据结构:xv6 必须建立一种合作机制防止它们互相干扰.即使是在单个处理器上,xv6 ...

  8. POJ3037 Skiing

    Skiing 题目大意: 给定一个M*N的网格,已知在每个网格中的点可以向上下左右四个方向移动一个单位,每个点都有一个高度值. 从每个点开始移动时存在一个速度值,从A点移动到B点,则此时B点的速度为& ...

  9. UML的关联(Association), 聚合(Aggregation), 组合(Composition)区别

    转载:http://blog.csdn.net/ocean181/article/details/6117369 UML的关联(Association), 聚合(Aggregation), 组合(Co ...

  10. hdu 4421 和poj3678类似二级制操作(2-sat问题)

    /* 题意:还是二进制异或,和poj3678类似 建边和poj3678一样 */ #include<stdio.h> #include<string.h> #include&l ...