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

思路:
一开始设置了两个变量,l和r分别代表左括号和右括号,后来发现和stack相关的操作只用设置一个变量即可,可是说是用来记录stack高度的也可以说是用来记录'{'值的。这样不仅更方便灵活,而且(我想不到合适的词语来描述。。)更容易激发灵感!

#include <iostream>
#include <string>
using namespace std; int main()
{
string ss;
int shift,l,K = ;
while(cin>>ss) {
if(ss[] == '-')
break;
l = ;
shift = ;
for(int i = ;i < ss.length();i++) {
if(ss[i] == '{')
l++;
else if(l)
l--;
else {
l++;
shift++;
}
}
cout<<++K<<". "<<l/+shift<<endl;
}
return ;
}

hdoj3351-stack的更多相关文章

  1. 线性数据结构之栈——Stack

    Linear data structures linear structures can be thought of as having two ends, whose items are order ...

  2. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  3. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  4. Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder

    Stack Overflow 排错翻译  - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...

  5. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

  6. Stack操作,栈的操作。

    栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...

  7. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  9. Stack的三种含义

    作者: 阮一峰 日期: 2013年11月29日 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词 ...

  10. Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)

    写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...

随机推荐

  1. Datatables+Bootstrap

    http://sandbox.runjs.cn/show/thwac3ec 运行效果 <!DOCTYPE html> <html lang="en"> &l ...

  2. 【转】 ios开发之倒计时实现的两种方法

    原文:http://blog.csdn.net/kylinbl/article/details/8972261 方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTime ...

  3. java_设计模式_适配器模式_Adapter Pattern(2016-08-09)

    概念 将一个接口转换成客户希望的另外一个接口.(该模式使得原本不兼容的类可以一起工作). UML图 适配器模式有类的适配器模式和对象的适配器模式两种不同的形式. (1)对象的适配器模式结构图 (2)类 ...

  4. hdoj 2601(判断N=i*j+i+j)

    Problem E Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  5. filter过滤器执行顺序

    浏览器请求---->进入过滤器---->进入doFilter方法--->执行chain.doFilter()方法就会放行----->进入业务逻辑方法------>进入过滤 ...

  6. gzip命令

    http://www.cnblogs.com/peida/archive/2012/12/06/2804323.html 减 少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时, ...

  7. 谷歌制图服务(Google Chart)接口生成二维码

    Google公布了制图服务(Google Chart)的接口,这项服务用起来相当简单,只使用浏览器就可以用来为统计数据自动生成图片. 目前谷歌制图服务提供折线图.条状图.饼图.Venn图.散点图.二维 ...

  8. windows 下PHP 和 nginx配置

    http://www.cnblogs.com/huayangmeng/archive/2011/06/15/2081337.html

  9. SAE 安装未包含的第三方依赖包

    如何使用virtualenv管理依赖关系 当你的应用依赖很多第三方包时,可以使用virtualenv来管理并导出这些依赖包,流程如下: 首先,创建一个全新的Python虚拟环境目录ENV,启动虚拟环境 ...

  10. delphi定义自己的消息

    定义一个消息需要两个步骤: 1.声明一个消息标识符 2.声明一个消息记录类型 一个消息标识符是一个整数大小的常数.Windows自用低于1024的消息,所以当你声明你自己的消息,你应该开始高于这一数字 ...