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. Caused by: java.lang.ClassNotFoundException: javassist.ClassPool

    1.错误原因 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  2. git config配置文件 (共有三个配置文件)

    设置 git status的颜色. git config --global color.status auto 一.Git已经在你的系统中了,你会做一些事情来客户化你的Git环境.你只需要做这些设置一 ...

  3. C# 标签打印示例 1

    初次写博客,有哪些不足的地方,还请多多指点,给予建议,谢谢! 如若想要源码,请留言.        本实例是在Webservice 中通过excel做模板来打印标签.具体需求是:一个订单一页纸打印4行 ...

  4. 事件监听:诀别Android繁琐的事件注册机制——view.setOnXXXXListener

    本版本为1.0,支持较少,使用不够方便.相关封装逻辑结构已升级至2.0,详情可参见:更完善的安卓事件监听实现 先简单扯两句这几天学习下来对java事件监听机制的一点感触.客观地讲,java的事件监听机 ...

  5. RadGrid SelectedIndexChanged 事件没反应的解决方法

    Hello Hrushikesh, You can set ClientSettings.EnablePostBackOnRowClick to true along with ClientSetti ...

  6. win7添加usb3.0驱动(错误代码1392,文件或目录损坏且无法读取)

    Win7添加usb3.0驱动 之前一直按照网上的方法执行dism命令挂载时,总是失败,错误代码1392,显示原因是文件或目录损坏且无法读取.这个错误以前在装机时老是出现导致系统安装不成功,在BIOS中 ...

  7. C++中使用stringstream简化类型转换

    C++标准库中的<sstream>提供了一个stringstream,以前基本没用过,突然发现很好用(^-^)V 参见 http://www.cplusplus.com/reference ...

  8. HDU Today--hdu2112

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. 在Wince模拟器接入网络的方法

    我第一次使用wince调用WCF服务的时候总是报错,找了半原因发现程序部署在模拟器中,而模拟器没有连接到网络,所以无法连接到WCF服务器. 以下是wince接入网络的方法:        1.点击模拟 ...

  10. Scala学习笔记--集合类型Queue,Set

    补充知识:http://www.importnew.com/4543.html 正文开始 scala.collection.immutable scala.collection.mutable 队列Q ...