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. VS2015 Cordova Ionic移动开发(二)

    一.创建空白Cordova应用 打开VS,选择[新建项目],选择其它语言JavaScript或者TypeScript,语言的话就按个人喜好,喜欢JS就用JS,喜欢TS就用TS,推荐使用TS书写,代码结 ...

  2. Index Full Scan vs Index Fast Full Scan-1103

    [Oracle] Index Full Scan vs Index Fast Full Scan作者:汪海 (Wanghai) 日期:14-Aug-2005 出处:http://spaces.msn. ...

  3. ASP.NET网站实现中英文转换(本地化资源)

    主要内容: 1. 简单例子 2. 进一步认识Localization 3. 语言转换 4. 解决方案 一. 简单例子 下面通过一个简单的例子来说明利用Localization来实现本地化是那么的简单, ...

  4. 12XML(可扩展标记语言)

    XML:eXtensible Markup Language 什么是标记语言?什么是标记? 标记(Markup):文档中任何不想被打印输出的部分(不是真正的文档内容,联想读书时做的“读书笔记”,在旁边 ...

  5. 移动触摸事件(touchstart、touchmove和touchend)

    touchstart事件:当手指触摸屏幕时候触发,即使已经有一个手指放在屏幕上也会触发. touchmove事件:当手指在屏幕上滑动的时候连续地触发.在这个事件发生期间,调用preventDefaul ...

  6. 启动scala的方法

    1.从官网 http://www.scala-lang.org/download/ 下载scala二进制通用版本以后,在终端命令行添加下载解压包的bin目录到环境变量: export PATH=/Us ...

  7. 最全Media 响应式 设置方法

    大家对于css3中media属性并不陌生,但是随着一些高视网膜的设备面世,很多情况对于media的不标准的用法也越来越多,我通过查找一些知识结合实践给总结出一些标准的设置的方法. CSS3 中的 Me ...

  8. 『重构--改善既有代码的设计』读书笔记----Hide Delegate

    所谓委托关系,就是一个类对于另一个类来说纯粹作为接口转发,即客户通过一个委托类去调用另一个对象.直白的委托关系就是委托类直接返回出目标类给客户调用,这个关系很麻烦,因为委托关系的变动就会影响客户端的代 ...

  9. HTML5 表单与文件

    -新增元素与属性 form.formaction.formmethod.placeholder(处于未输入状态时文本框显示的输入提示).autofocus(自动获取光标焦点).list(该属性的值为某 ...

  10. Become a Windows Insider and Test New Windows 10 Features

    SR: To write an Edge browser extension. Microsoft is releasing Windows 10 build 14291 with browser e ...