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,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
随机推荐
- Cookie和Session在Node.JS中的实践(三)
Cookie和Session在Node.JS中的实践(三) 前面作者写的COOKIE篇.SESSION篇,算是已经比较详细的说明了两者间的区别.机制.联系了.阅读时间可能稍长,因为作者本身作图也做了不 ...
- cake-walk
Of course it was not a cake-walk in the beginning 3. This is going to be a cakewalk 这将易如反掌. 4. Julia ...
- hdu 1599 find the mincost route 最小环
题目链接:HDU - 1599 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须 ...
- Java类的定义及其实例化
如果你不了解类和对象的概念,请猛击这里:Java类和对象的概念 类必须先定义才能使用.类是创建对象的模板,创建对象也叫类的实例化. 下面通过一个简单的例子来理解Java中类的定义: public cl ...
- ubifs笔记
第1章 UBIFS UBIFS不是工作在块在设备之上,所以UBIFS不能用于MMC之类的设备. 与传统的flash文件不同,UBIFS不是工作是块设备之上.传统的flash文件系统(如Jffs2)工作 ...
- 控制面板cpl大全
ALSNDMGR.CPL AC97 Audio组态设定appwiz.cpl 添加和删除程序bthprops.cpldesk.cpl 显示属性firewall.cpl Windows防火墙hdwwi ...
- 关于iframe的高度自适应问题(js)
function SetCwinHeight() { var cwin=document.getElementById("cwin"); if (document.getEleme ...
- 今天终于看了一下tanh函数的形式,双曲正切函数
tanh = sinh / cosh sinh = Hyperbolic sin cosh = Hyperbolic cos
- 面试题:Add Two Numbers(模拟单链表)
题干: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- 机器学习中的范数规则化之 L0、L1与L2范数
http://blog.csdn.net/zouxy09/article/details/24971995/ L1正则化及其推导 Laplace(拉普拉斯)先验与L1正则化 今天我们聊聊机器学习中出现 ...