F——宋飞正传(HDU3351)
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)的更多相关文章
- 栈 <stack> F - 宋飞正传
I’m out of stories. For years I’ve been writing stories, some rather silly, just to make simple prob ...
- HDU 3351 Seinfeld 宋飞正传(水)
题意: 给出一个串,串内只有大括号,问经过几次改变可使全部括号合法?改变指的是可以将某一方向的括号变成另一方向. 思路: 利用栈的特点,若出现成对的合法括号,直接删掉,留下那些不合法的成为一串.既然不 ...
- NSDate的处理:前一天、后一天等关于时区偏移的处理以及在数据库中的使用
看来非常多网上关于日期的处理,今天.昨天.前天.后天.一周等,满心欢喜的拿着去验证结果总是不尽如人意,看别别人的代码看的脑涨.自己就写一个简单的,自己来用,以后用其它的方法,我会在完好,再次记录.以方 ...
- 透过表象看本质!?之三——Kalman滤波
数据拟合能够估计出数据变化的趋势,另外一个同等重要的应用是如何利用这一趋势,预测下一时刻数据可能的值.通俗点儿说,你观察苍蝇(蚊子,蜜蜂)飞了几秒,你也许会想“它下一个时刻可能在哪儿”,“呈现出什么样 ...
- LoadRuner12.53教程(三)
教训1:建立一个Vuser Script jiào教 xùn训 1 : jiàn建 lì立 yī一 gè个 V u s e r S c r ...
- LoadRuner12.53教程(二)
使用HP Web访问示例应用程序 shǐ使 yòng用 H P W e b fǎng访 wèn问 shì示 lì例 yìng应 yòng用 chén ...
- SESSION和cookie的使用和区别
PHP中SESSION和cookie的使用和区别 cookie 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制. PHP在http协议的头信息里发送cookie, 因此 setcookie( ...
- 11.python3标准库--使用进程、线程和协程提供并发性
''' python提供了一些复杂的工具用于管理使用进程和线程的并发操作. 通过应用这些计数,使用这些模块并发地运行作业的各个部分,即便是一些相当简单的程序也可以更快的运行 subprocess提供了 ...
- asyncio:python3未来并发编程主流、充满野心的模块
介绍 asyncio是Python在3.5中正式引入的标准库,这是Python未来的并发编程的主流,非常重要的一个模块.有一个web框架叫sanic,就是基于asyncio,语法和flask类似,使用 ...
随机推荐
- GitHub高级搜索指南
还在为自学时找不到适合练手的项目而苦恼? 还在好奇别人是如何在GitHub众多项目中找到高质量代码的? 真的是因为他们独具慧眼吗? 不,其实他们只是掌握了正确的搜索方法. 下面介绍几种常用的GitHu ...
- java 语句流程
一.if条件判断 太简单,不加说明 二.switch条件选择语句 注意: 1.switch(表达式),表达式的数据类型 byte/short/char/int/String/enum 2.别忘记写br ...
- Windows10系统配置telnet服务的方法
通常情况下,Windows10正式版系统的telnet服务都是处于关闭状态的,需要我们手动开启才可以.telnet服务可以调试端口,其重要性不容小视.今天,系统城小编就教大家如何配置telnet服务. ...
- java异步调用方法
一.利用多线程 直接new线程 Thread t = new Thread(){ @Override public void run() { longTimeMethod(); } }; 使用线程池 ...
- ruby 模块 respond_to
def hi puts 'hi friend' end module Amodule def self.hello puts 'hello friend' end end def rsp(txt) p ...
- kuangbin专题专题十一 网络流 Dining POJ - 3281
题目链接:https://vjudge.net/problem/POJ-3281 题目:有不同种类的食物和饮料,每种只有1个库存,有N头牛,每头牛喜欢某些食物和某些饮料,但是一头牛 只能吃一种食物和喝 ...
- 团队项目-Beta冲刺(第一周)
团队项目-Beta冲刺(第一周) 一. 作业描述 这个作业属于哪个课程 这个作业要求在哪里 团队名称 CTRL-IKun 这个作业的目标 合理安排时间完成接下来的任务,剩余任务预估,分配任务(开发,测 ...
- 【WPF学习】第二十章 内容控件
内容控件(content control)是更特殊的控件类型,它们可包含并显示一块内容.从技术角度看,内容控件时可以包含单个嵌套元素的控件.与布局容器不同的是,内容控件只能包含一个子元素,而布局容器主 ...
- ios--->特定构造方法NS_DESIGNATED_INITIALIZER
特定构造方法 1> 后面带有NS_DESIGNATED_INITIALIZER的方法,就是特定构造方法 2> 子类如果重写了父类的[特定构造方法],那么必须用super调用父类的[特定构造 ...
- 解决IDEA使用lombok注解无效,@Data不生效问题
在settings设置启用注解即可: