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. asp.net mvc生命周期学习

    ASP.NET MVC是一个扩展性非常强的框架,探究其生命周期对用Mock框架来模拟某些东西,达到单元测试效果,和开发扩展我们的程序是很好的. 生命周期1:创建routetable.把URL映射到ha ...

  2. C# 通过hessian调Java注意事项

    照理说C#可以通过标准的web服务可以轻松地调用Java,但是鉴于hessian的高性能及开发效率,个人认为C#通过hessian调用java是很值得提倡的.之前完成的一个比较大型的企业应用项目就是采 ...

  3. node http.get请求

    var http = require('http'); var querystring = require('querystring') var url = 'http://www.baidu.com ...

  4. 平衡搜索树(一) AVL树

    AVL树 AVL树又称为高度平衡的二叉搜索树,是1962年有俄罗斯的数学家G.M.Adel'son-Vel'skii和E.M.Landis提出来的.它能保持二叉树的高度 平衡,尽量降低二叉树的高度,减 ...

  5. php中的JSON中文处理

    最近在PHP中要输出JSON,上网查了一下,对中文支持不太好,要不就先转成utf-8的编码,再用json_encode生成,客户端还要再utf-8转中文.对于网页已经用GB2312的服务器,不想这样折 ...

  6. SGU 221.Big Bishops(DP)

    题意: 给一个n*n(n<=50)的棋盘,放上k个主教(斜走),求能放置的种类总数. Solution : 同SGU 220,加个高精度就好了. code #include <iostre ...

  7. 【HDU3487】【splay分裂合并】Play with Chain

    Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...

  8. 基于php-fpm的配置详解[转载]

    php自带php-fpm/usr/local/php/etc/php-fpm.confpid = run/php-fpm.pidpid设置,默认在安装目录中的var/run/php-fpm.pid,建 ...

  9. ng-class css样式

    <style> .error{background-color: red;} .warning{background-color: yellow;} </style> < ...

  10. 《python学习手册》之一——程序运行

    Python解释器执行Python代码时候,大概经历如下几个阶段:(1) 加载代码文件 (2)翻译成AST (3)生成bytecode(.pyc文件,与编译的python版本有关).可以使用pytho ...