Parentheses Balance 

You are given a string consisting of parentheses () and []. Astring of this type is said to be correct:

(a)
if it is the empty string
(b)
if A and B are correct, AB is correct,
(c)
if A is correct,
(A
) and
[A
] is correct.

Write a program that takes a sequence of strings of this type and checktheir correctness. Your program can assume that the maximum stringlength is 128.

Input

The file contains a positive integer
n and a sequence of
n strings ofparentheses
() and
[], one string a line.

Output

A sequence of
Yes or
No on the output file.

Sample Input

3
([])
(([()])))
([()[]()])()

Sample Output

Yes
No
Yes

题意: 匹配小括号和中括号~

做法及其注意点:

一种情况是遇到 '(' 或 '[', 那么直接放到栈里不解释~

另一种情况是遇到 ')' 或 ']', 进行判定, 是否前一个是 '(' 或 '[' (注意了, 此时栈也许是空的), 若是, 弹出栈~

最后判断是否为空~

AC代码:

#include<stdio.h>
#include<stack> using namespace std; int main() {
int T;
char ch;
scanf("%d", &T);
getchar();
while(T--) {
stack <char> sta; while(scanf("%c", &ch)) {
if(ch == '\n')
break;
if(sta.empty()) {
sta.push(ch);
continue;
}
if(ch == '(')
sta.push(ch); if(ch == ')') {
if(sta.top() == '(')
sta.pop();
else
sta.push(ch);
}
if(ch == '[')
sta.push(ch); if(ch == ']') {
if(sta.top() == '[')
sta.pop();
else
sta.push(ch);
}
} if(sta.empty())
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

UVA 673 (13.08.17)的更多相关文章

  1. UVA 536 (13.08.17)

     Tree Recovery  Little Valentine liked playing with binary trees very much. Her favoritegame was con ...

  2. UVA 10194 (13.08.05)

    :W Problem A: Football (aka Soccer)  The Problem Football the most popular sport in the world (ameri ...

  3. UVA 253 (13.08.06)

     Cube painting  We have a machine for painting cubes. It is supplied withthree different colors: blu ...

  4. UVA 573 (13.08.06)

     The Snail  A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...

  5. UVA 10499 (13.08.06)

    Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...

  6. UVA 10025 (13.08.06)

     The ? 1 ? 2 ? ... ? n = k problem  Theproblem Given the following formula, one can set operators '+ ...

  7. UVA 465 (13.08.02)

     Overflow  Write a program that reads an expression consisting of twonon-negative integer and an ope ...

  8. UVA 10494 (13.08.02)

    点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...

  9. UVA 424 (13.08.02)

     Integer Inquiry  One of the first users of BIT's new supercomputer was Chip Diller. Heextended his ...

随机推荐

  1. SRM 588 D2 L2:GUMIAndSongsDiv2,冷静思考,好的算法简洁明了

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12707 算法决定一切,这道题目有很多方法解,个人认为这里 ve ...

  2. Oracle SQL ANY和ALL语句

    Oracle的嵌套子查询可以使用Some,Any和All对子查询中返回的多行结果进行处理. Some表示满足其中一个的含义,是用or串起来的比较从句. 例如:SELECT * FROM emp WHE ...

  3. JavaScript的数据类型转换

    首先,由于JavaScript是弱类型语言(弱类型的语言的东西没有明显的类型,他能随着环境的不同,自动变换类型而强类型则没这样的规定,不同类型间的操作有严格定义,只有相同类型的变量才能操作,虽然系统也 ...

  4. C语言结构体的字节对齐

    Test Code: #include <iostream> #include <cstring> using namespace std; struct A{ int a; ...

  5. c#串口编程和单片机通信重大发现

    1.遇到问题时看看这里 //每次响应中断结束后清空缓存,防止在显示关闭时,打开后又一次性出现 serialPort1.DiscardInBuffer();

  6. hdu5355 Cake(构造)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Cake Time Limit: 2000/1000 MS (Java/Other ...

  7. Bootstrap定制(一)less入门及编译

    第一篇博,希望支持. 近期在开发一个项目,项目前端定位于bootstrap,遂花了少许时间研究了bootstrap,将其整理整理,与众人共享. bootstrap官方的定制,功能还算完善,但是基于we ...

  8. ubuntu没有进入图形界面解决办法

    可以通过设置runlevel 为2 来控制以后的登陆,或者是升级不完全.中间出错了,无法正常登陆.有2种方式来进入图形界面: 1. 登陆系统后,输入如下命令来启动图形界面: startx 2. 登陆系 ...

  9. Lua 字符串函数小结

    1.求字符串长度 string.len(str) 2.大小写转换 string.upper(str) string.lower(str) 3.字符串查找(非全局) --func_string.lua ...

  10. 如何让checkbox复选框只能单选

    function框架div 如何让checkbox复选框只能单选 在项目开发中遇到一个这样的问题,要让一列复选框架在任何时间段内只能选择一个. 有人说怎么不用单选框了,因为单选框一旦选择了就不能取消选 ...