hdoj 3351 Seinfeld 【栈的简单应用】
Seinfeld
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.
of even length.
The last line of the input is made of one or more ’-’ (minus signs.)
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.
}{
{}{}{}
{{{}
---
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 【栈的简单应用】的更多相关文章
- hdu Train Problem I(栈的简单应用)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- 栈回溯简单实现(x86)
0x01 栈简介 首先局部变量的分配释放是通过调整栈指针实现的,栈为函数调用和定义局部变量提供了一块简单易用的空间,定义在栈上的变量不必考虑内存申请和释放.只要调整栈指针就可以分配和释放内存. ...
- Javascript的堆和栈的简单理解
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 栈及其简单应用(二)(python代码)
一.括号判定 前一篇文章我们介绍了栈的简单应用中,关于括号的判定,但那只是一种括号的判定,下面我们来介绍多种括号混合使用时,如何判断括号左右一一对应. 比如“{}{(}(][”这种情况,需要对一种括号 ...
- 栈及其简单应用(python代码)
栈属于线性结构(Linear Struncture),要搞清楚这个概念,首先要明白”栈“原来的意思,如此才能把握本质."栈“者,存储货物或供旅客住宿的地方,可引申为仓库.中转站,所以引入到计 ...
- HDU 3351 Seinfeld(括号匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3351 解题报告:输入一个只有'{'跟'}'的字符串,有两种操作,一种是把'{'变成'}',另一种是'} ...
- HDOJ 1022 模拟栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)
栈的练习,如此水题竟然做了两个小时... 题意:给出矩阵大小和矩阵的运算顺序,判断能否相乘并求运算量. 我的算法很简单:比如(((((DE)F)G)H)I),遇到 (就cnt累计加一,字母入栈,遇到) ...
- HDU 3351 Seinfeld 宋飞正传(水)
题意: 给出一个串,串内只有大括号,问经过几次改变可使全部括号合法?改变指的是可以将某一方向的括号变成另一方向. 思路: 利用栈的特点,若出现成对的合法括号,直接删掉,留下那些不合法的成为一串.既然不 ...
随机推荐
- iview upload on-format-error 事件 在 before-upload 事件 之后,导致在before里面阻止上传后,监测事件失效,需要自己手工写
iview upload on-format-error 事件 在 before-upload 事件 之后,导致在before里面阻止上传后,监测事件失效,需要自己手工写
- CAD交互绘制直线(网页版)
用户可以在CAD控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE ...
- JavaScript设计模式基础之面向对象的JavaScript(一)
动态语言类型与鸭子类型 此内容取自JavaScript设计模式与开发实践一书 编程语言按照数据类型大体可以分为2类,一类就是静态类型语言,另一类则是动态类型语言 静态类型语言也可以称之为编译语言,而动 ...
- tabs标签页的数据缓存
一进入tabs标签页默认就将所有标签页的数据请求到,并渲染到页面上, 这样如果数据量太大的话会渲染很久, 我的需求就是点击不同的标签时再请求数据,同时对点击过的标签页数据进行缓存,下次点击时不再重新请 ...
- MySQL索引之博客荐读
推荐博客: 寒江独钓. 浅谈算法和数据结构: 十 平衡查找树之B树 张洋. MySQL索引背后的数据结构及算法原理 漫画算法:什么是 B+ 树? B树和B+树的插入.删除图文详解 Jeremy Col ...
- 【eclipse】使用说明
一. 设置jdk版本 1. Window->Preferences->Java->Installed JREs,选择jdk安装目录: 2. Window->Preference ...
- Python的串口
要使用python中的串口,可以下载pywin32-224-cp36-cp36m-win_amd64.whl去安装或者pip install去安装. 调试下来,有一点很不爽,读取read()数据的ti ...
- python mock模块使用(一)
什么是mock unittest.mock是一个用于在Python中进行单元测试的库,Mock翻译过来就是模拟的意思,顾名思义这个库的主要功能是模拟一些东西. 它的主要功能是使用mock对象替代掉指定 ...
- Dream City(线性DP)
描述 JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the ya ...
- 公路修建(Prim)
洛谷传送门 这道水题告诉了我,堆优化的prim有时还不如朴素prim快... 居然记错时间复杂度了,我也真是菜. #include <cstdio> #include <queue& ...