1410282310-hd-Seinfeld



Seinfeld

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

Total Submission(s): 1413    Accepted Submission(s): 697

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

题目大意

有一堆括号,{}这样算配对,{{,}},}{这样都算不配对,对于一组不配对的括号,可以通过变化括号的方向来使其配对,变一个算一次,求最少的变化次数。

解题思路

对于{}这种情况,可以将其略去。这样不配对的括号组可以有以下三种情况1.{{{{2.}}}}3.}}}}{{{{当然也可以合并为一种情况n...}{...m。对于}}或者{{只需改变一次,而对于}{则须改变两次。

这道题主要用到了栈的知识。

代码

#include<stdio.h>
#include<string.h>
char s[2200],c[2200];
int main()
{
	int len1,len2,len3;
	int i,j,k=1;
	int sum,top;
	while(scanf("%s",s)&&s[0]!='-')
	{
		len1=strlen(s);
		top=1;
		memset(c,0,sizeof(c));
		c[0]=s[0];
		for(i=1;i<len1;i++)
		{
			c[top]=s[i];
			//if(s[i-1]=='{'&&s[i]=='}')   错误之处
			//测试数据{{}}
			if(c[top-1]=='{'&&c[top]=='}')
			//是否入栈是让栈组比较的。
			    top--;
			else
				top++;
		}
		len2=0;
		for(i=0;i<top;i++)
		{
		    if(c[i]=='}')
		        len2++;
		    else
		        break;
		}
		len3=top-len2;
		/*if(len1%2==0)
		    sum=len1/2+len2/2;
		else
		    sum=len1/2+len2/2+2;
		这样判断的话要额外考虑栈为空的情况*/
		sum=(len2+1)/2+(len3+1)/2;
		printf("%d. ",k);
		k++;
		printf("%d\n",sum);
	}
	return 0;
} 
时间: 2024-12-16 13:00:43

1410282310-hd-Seinfeld的相关文章

怎么用HD Tune检测硬盘坏道

HD Tune软件不仅小巧而且很易使用,是一款检测电脑硬盘的优良工具.不仅是电脑硬盘,包括移动硬盘在内一样可以检测.那么,如何使用HD Tune呢?如何使用HD Tune检测磁盘坏道呢? 工具/原料 HD Tune软件一枚 使用HD Tune检查硬盘坏道 1.百度搜索下载HD Tune软件,注意:尽量选择系统之家官网上下载这类软件,因为系统之家上的软件不会捆绑的流氓软件 2.软件打开之后,界面如图,最上方显示硬盘的厂家信息,图示红色遮挡区 3.点击选项卡切换至错误扫描,如图,然后在红框位置,尽量

HDU3351 Seinfeld 【贪心】

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

uva 12452 Plants vs. Zombies HD SP (树DP)

Problem I: Plants vs. Zombies HD Super Pro Plants versus Zombies HD Super Pro is a game played not a grid, but on a connected graph G with no cycles (i.e., a tree). Zombies live on edges of the tree and chew through edges so that tree falls apart! Pl

OSX: 解决10.10 Yosemite Developer Preview 2安装后的Recovery HD看不见的问题

OS X 10.10 Yosemite Developer Preview 2,下面简称10.10DP2,安装之后,有可能出现10.10的恢复盘(Recovery HD)看不见的问题.这是因为它的一个设置错误,下面我们用比较直观的方法来解决它. 这是修复后的启动选项,其中显示的Recovery-10.10 清楚点的 首先,开启磁盘工具的Debug菜单. 无论是从哪个磁盘启动,都可以操作,运行下面命令: defaults write com.apple.DiskUtility DUDebugMen

Radeon HD 7850 vs Radeon R9 270X

Radeon HD 7850 vs Radeon R9 270X HW compare Intro The Radeon HD 7850 comes with a GPU core speed of 860 MHz, and the 2048 MB of GDDR5 RAM runs at 1200 MHz through a 256-bit bus. It also is made up of 1024 SPUs, 64 Texture Address Units, and 32 Raster

JWPlayer支持SD/HD

First, we have to render two versions of our video: an SD (standard definition) and HD (high definition) one. Since the newer iPads, iPhones and Androids all play 720p video, let’s use that as the HD version. Full HD (1080p) is still a bit overkill,

电影TS/TC/SCR/R5/BD/HD版本意思收集(转)

一.版本说明: 1.CAM(枪版) CAM通常是用数码摄像机从电影院盗录.有时会使用小三角架,但大多数时候不可能使用,所以摄像机会抖动.因此我们看到画面通常偏暗人物常常会失真,下方的 字幕时常会出现倾斜. 由于声音是从摄像机自带的话筒录制,所以经常会录到观众的笑声等声音.因为这些因素,图象和声音质量通常都很差. 2.TS(准枪版) TS是TELESYNC的缩写.TS与CAM版的标准是相同的.但它使用的是外置音源(一般是影院座椅上为听力不好的人设的耳机孔)这个音源不能保证是好的音源,因为受到很多背

千里之堤毁于蚁穴(慎用HD Wallets)

转自:http://blog.sina.com.cn/s/blog_12ce70a430102vbu9.html -- 随机系列谈之四 现在我们都该明白,无论怎样强调随机对于比特币的重要性都不为过,随机的确称得上是比特币的“命根子”.在过去的几篇文章中,小太已经介绍过了一些因随机问题可能导致私钥暴漏的情况,今天,我再跟大家聊聊分层确定性(Hierarchical Deterministic)钱包. HD Wallets因其“只需要一个主(根)私钥,就能生成海量子私钥”这个特性广受欢迎,在过去的一

Intel HD Graphics 620 (华硕FL5900U) Windows7下安装显卡驱动

我们的牙膏大厂Intel,不但会挤牙膏,现在连驱动也不给你用了.KabyLake也就是第七代酷睿处理器,集成了Intel HD Graphics 620核显,核显(显卡)驱动程序只支持到Win10这一个系统,之前的Win8,Win8.1,Win7等,统统不支持.而华硕的FL5900U这款笔记本,就使用I7-7500U,刚好是第七代酷睿,集成了620核显. 还好伟大的YouTube上有人发出了如何在Win7下安装驱动的方法,具体如下: 下载Zip格式的驱动,解压缩,找到Graphics文件夹,找到

OSX: 命令行制作U盘Recovery HD

使用命令行操作,非常easy,可是操作不当非常危急! 免责声明:假设操作不当造成的数据丢失,本人概不负责. 为什么? 不是有OSX恢复磁盘助理嘛?是的.假设仅仅想使用GUI的软件.能够去苹果官方站点:http://support.apple.com/kb/DL1433?viewlocale=zh_CN 唯一的原因就是:生命不息,折腾不止:只是还有另外一个诱因,就是看看究竟官方的恢复磁盘助理的原理,换句话说,用自己的办法达到相同的效果. 前提:你有一个工作的Recovery HD,你的Mac支持U