题目:
 
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. 

InputYour 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.)

OutputFor 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 解题思路:
很显然这一题要使用栈来储存括号。
难点:要使改变的括号最少。如果输入的是’{‘则放入栈内,如果输入的是’}‘则要看栈顶是否是’{‘,有,则删除栈顶括号;没有,则把’}’变为‘{’放入栈顶(改变次数加一)。结束时栈内只可能存在这种‘{’括号,则只需要再改变(‘{’个数)/2个括号即可。
 #include <iostream>
#include <stack>
#include <cstdio>
#include <cstring>
using namespace std; int main()
{
int i,k,n,t=,ans,j;
char a[];
stack<char>s; while ()
{
memset(a,,sizeof(a));
gets(a);ans=;
if (a[]=='-')break;
for (j=;j<strlen(a);j++)
{
if (a[i]=='}')
{
if (s.top()=='{')
s.pop();
if (!s.size()) //如果栈内元素个数为0
{
a[i]='{'; //改变括号
ans++;
}
}
if (a[i]=='{')s.push(a[i]);
}
k=s.size()/;
ans+=k;
cout <<t<<". "<<ans<<endl;
t++;
}
return ;
}


F——宋飞正传(HDU3351)的更多相关文章

  1. 栈 <stack> F - 宋飞正传

    I’m out of stories. For years I’ve been writing stories, some rather silly, just to make simple prob ...

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

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

  3. NSDate的处理:前一天、后一天等关于时区偏移的处理以及在数据库中的使用

    看来非常多网上关于日期的处理,今天.昨天.前天.后天.一周等,满心欢喜的拿着去验证结果总是不尽如人意,看别别人的代码看的脑涨.自己就写一个简单的,自己来用,以后用其它的方法,我会在完好,再次记录.以方 ...

  4. 透过表象看本质!?之三——Kalman滤波

    数据拟合能够估计出数据变化的趋势,另外一个同等重要的应用是如何利用这一趋势,预测下一时刻数据可能的值.通俗点儿说,你观察苍蝇(蚊子,蜜蜂)飞了几秒,你也许会想“它下一个时刻可能在哪儿”,“呈现出什么样 ...

  5. LoadRuner12.53教程(三)

    教训1:建立一个Vuser Script jiào教   xùn训   1   :   jiàn建   lì立   yī一   gè个   V   u   s   e   r   S   c   r ...

  6. LoadRuner12.53教程(二)

    使用HP Web访问示例应用程序 shǐ使   yòng用   H   P   W   e   b   fǎng访   wèn问   shì示   lì例   yìng应   yòng用   chén ...

  7. SESSION和cookie的使用和区别

    PHP中SESSION和cookie的使用和区别 cookie 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制. PHP在http协议的头信息里发送cookie, 因此 setcookie( ...

  8. 11.python3标准库--使用进程、线程和协程提供并发性

    ''' python提供了一些复杂的工具用于管理使用进程和线程的并发操作. 通过应用这些计数,使用这些模块并发地运行作业的各个部分,即便是一些相当简单的程序也可以更快的运行 subprocess提供了 ...

  9. asyncio:python3未来并发编程主流、充满野心的模块

    介绍 asyncio是Python在3.5中正式引入的标准库,这是Python未来的并发编程的主流,非常重要的一个模块.有一个web框架叫sanic,就是基于asyncio,语法和flask类似,使用 ...

随机推荐

  1. MySQL 行列相互转换

    行列相互转换 /*创建表*/ CREATE TABLE ic ( NAME ), Product ), amount INT ); INSERT INTO ic VALUES (), (), (), ...

  2. ORM基础3 在python脚本里调用Django环境

    1.查询 1.# all获取所有的object,结果QuerySet,列表 print('all'.center(80, '=')) ret = models.Person.objects.all() ...

  3. [bzoj4443] [loj#2006] [洛谷P4251] [Scoi2015]小凸玩矩阵

    Description 小凸和小方是好朋友,小方给小凸一个 \(N \times M\)( \(N \leq M\) )的矩阵 \(A\) ,要求小秃从其中选出 \(N\) 个数,其中任意两个数字不能 ...

  4. 「 扫盲 」Web服务器和应用服务器的区别

    我们经常使用apache,tomcat,nginx,jetty等服务器,但并不清楚它们间的区别,它们中,哪些是Web服务器,哪些是应用服务器?今天就来告诉你 Web服务器 理解WEB服务器,首先你要理 ...

  5. 发现 一个业务管理系统 解决了 orm 和 前端框架 剩下的 是 业务逻辑 了 。 哈

    解决了 orm 和 前端框架 剩下的 是 业务逻辑 了 . 哈 还有 各种 aop 组件 呢 . 大家 high 来 准备 用 fluent data  和 mysql 写一个 wcf 的 接口呢. ...

  6. Redis(七):set/sadd/sismember/sinter/sdiffstore 命令源码解析

    上两篇我们讲了hash和list数据类型相关的主要实现方法,同时加上前面对框架服务和string相关的功能介绍,已揭开了大部分redis的实用面纱. 现在还剩下两种数据类型: set, zset. 本 ...

  7. CentOS7 zabbix4.0搭建配置

    一.Zabbix-Server服务器端的安装 概述:10050是Agent的端口,Agent采用被动方式,Server主动连接Agent的10050端口:10051是Server的端口,Agent采用 ...

  8. AVLTree(C++实现)没有统一旋转操作

    最近疫情比较严重,只能在家里休息,利用休息之余,我用C++把AVL树实现了一遍 大学老师只讲一些比较简单的数据结构和算法,这些高级数据结构还是需要自己主动学习并且动手来实现的, 从前只听说过AVLTr ...

  9. mysql--->profile使用

    Mysql分析-profile详解 简介 Profiling是从 mysql5.0.3版本以后才开放的. 启动profile之后,所有查询包括错误的语句都会记录在内. 此工具可用来查询SQL执行状态, ...

  10. CTF--HTTP服务--暴力破解

    开门见山 1. 扫描靶机ip,发现PCS 192.168.1.103 2. 用nmap扫描靶机开放服务和服务版本 3. 再扫描全部信息 4. 用nikto探测敏感文件 5. 打开敏感网页发掘信息 6. ...