HDU3351 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
题意:给定一个括号序列,求最少经过多少操作能使其变为合法序列。操作仅仅能是把左括号变为右括号或者右括号变为左括号。
题解:每次读取一个字符,若是左括号。那么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 【贪心】的更多相关文章
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【BZOJ-4245】OR-XOR 按位贪心
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
- 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心
SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...
- 【贪心】HDU 1257
HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
随机推荐
- SQL DISTINCT 用法(去重)
现在以下数据 查有出现的TeacherId SELECT TeacherID FROM Student 结果 有重复的ID出现 确实查法: SELECT TeacherID FROM Student ...
- UITableView中的dequeueReusableCellWithIdentifier的方法
在使用UITableView控件的时候,datasource的代理方法经常会使用到下面的方法来加载UITableView的数据显示 - (UITableViewCell *)tableView:(UI ...
- Ubuntu 16.04将系统时间写入到硬件时间BIOS
说明:在Ubuntu中为了和Windows保持一致,会将系统时间设置成CST的,所以下面的说法是设置成UTC的问题是由于所在的环境不一致导致的,本章只讨论如何设置时间到BIOS,不做时区分析,下面忽略 ...
- jquery验证表单代码
代码如下: //开始验证-修改用户密码 $('.editUserPwd').validate({ /**//* 设置验证规则 */ rules: { lname:{ required:true, st ...
- apache2.2 到 2.4后配置文件需要更改的部分
参考: http://www.dotblogs.com.tw/maple ... e24_httpd_conf.aspx 1. 访问控制2.2 的时候Order deny,allowDeny fro ...
- osg节点统计方法(点数 面数) 【转】
void statusNode(osg::ref_ptr<osg::Node> node,int& verNum,int& faceNum){ osg::ref_ptr&l ...
- 2017.8.5 Linux达人养成计划 I (上)
参考来自:http://www.imooc.com/learn/175 1 linux简介 1.1 linux简介 linux分为了内核版本和发行版本. 二者的区别:内核版本是由官方提供,而不同的发行 ...
- ant create-path 报不允许匹配[xX][mM][1L]错误
将build.xml配置文件的第一行 <?xml version="1.0" encoding="UTF-8"?> 空格去掉
- 不删除记录的表CRUD的常见处置
为什么不删除记录,因为这些记录只是暂时不用了,以后还是有可能会用到的,比如说统计:另外一些主键外键依赖级联删除的场合也不好真删的,容易批量删除.真删了就不容易恢复回来了. 一般做法是,增加一个avai ...
- 在html页面中直接嵌入图片数据
一般情况,通常是在html页面中应用图片的链接,如: <img src="http://baidu.com/logo.gif"> 但是,这样的前提是我们需要将图片先 ...