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. Wireshark提示没有一个可以抓包的接口

    这是由于win下默认NPF服务是关闭的,需要以管理员的身份开启这个服务 Windows上安装wireshark时,会遇到NPF驱动无法启动的情况,一般如果采用管理员的方式就可以正常启动,或者是将NPF ...

  2. CSRF 攻击的应对之道

    转载自imb文库 CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,该攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在 ...

  3. JSONModel的基本使用

    JSONModel 是一个库,它能智能并且快速的创建出数据 model,你可以在你的 iOS 项目或者 OSX 项目上使用它. 使用前准备 添加 JSONModel 到你的工程中 1.需要的环境: A ...

  4. xcode中如何安装多个版本的模拟器

    在xcode里面,安装的时间默认自带的有模拟器,有时间为了调试需要使用个多个版本的模拟器 在xcode  -> preference  里面 选择download,这里你可下载你需要的模拟器

  5. 【转】WF事件驱动

    转自:http://www.cnblogs.com/Mayvar/category/315963.html 这系统的教程有代码可以下载 WF事件驱动(5) 摘要: 之前,我通过4篇文章介绍了在WF4中 ...

  6. imod报错:error while loading shared libraries: libjpeg.so.62的解决办法

    the file libjpeg.so.62(in /usr/lib/libjpeg.so.62)belongs to the package libjpeg62so try to reinstall ...

  7. css去除webkit内核的默认样式

    做移动端的朋友应该知道,iphone的默认按钮是个很恶心的圆角,select下拉框也有默认样式无法修改. 这时候可以用到 -webkit-appearance:none //去除默认样 在按钮和sel ...

  8. js获取当前时间戳与日期比较

    如何用javascript获取当前时间戳: 复制代码 代码示例: 方法1: var timestamp = date.parse(new date()); 结果:1280977330000 方法2: ...

  9. JavaScript生成器+随机数的使用

    function* getIndex(indexList){ var len = indexList.length; var m; while(indexList.length > 0){ m ...

  10. 转:Google技术开发指南:给大学生自学的建议

    原文来自于:http://blog.jobbole.com/80621/ 技术开发指南 想要成为成功的软件工程师,必须拥有坚实的计算机科学的基础.本指南针对大学生,给出一条自学途径,让学生以科班和非科 ...