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. HDU ACM 1078 FatMouse and Cheese 记忆化+DFS

    题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...

  2. UVALive 4043 Ants

    KM   构图求最小权值匹配 保证最小的权值,所连的边一定是能够不相交的. Ants Time Limit: 3000MS   Memory Limit: Unknown   64bit IO For ...

  3. c++11 生产者/消费者

    下面是一个生产者消费者问题,来介绍condition_variable的用法.当线程间的共享数据发生变化的时候,可以通过condition_variable来通知其他的线程.消费者wait 直到生产者 ...

  4. 代码,显示IPhone剩余磁盘空间

    #include <sys/mount.h> //这段代码示范怎么取得 iPhone 的剩余磁盘空间,还有全部磁盘空间 long long freeSpace() { struct sta ...

  5. (转)用来理解Java的8个图表

    很多时候,一张图比你说 1000 个字能更有效的说清楚一个问题.我们列举了 8 个关于 Java 语言的图表,或许可以让你对 Java 有着更深入的认识. 1.字符串不变性(String Immuta ...

  6. 文本框按键事件onkeydown、onkeypress、onkeyup区别

    当我们在搜索时,会用到这几个事件 onkeydown 是指鼠标按下的那一刻,此时用户不知道按了什么,文本框也不会显示,首先触发的事件 onkeypress 是指鼠标按下然后松开的瞬间,此时仍然获取不到 ...

  7. Android-图标

    首先需要申明一点,系统图标并不存在于项目资源中,而是存在于设备中. 这就带来一个大问题,界面风格的控制权交到了不同的设备手中.这是我们不愿意看到的. 如何解决这个问题?有两种方法: 1.创建自己的图标 ...

  8. WIN7下使用.net(C#)监视剪贴板 (转)

    最近需要做一个小程序,需要常驻后台,监视剪贴板变化并提取内容, 在网上查了一些资料,先采用SetClipboardViewer方法实现,具体原理可以参考http://www.cnblogs.com/j ...

  9. First 5 minutes of SQLite

    What is SQLite? SQLite is light-weight RDBMS, it is use the file system rather than the C/S client, ...

  10. 控制用户的访问之权限、角色【weber出品必属精品】

    权限的作用 限制用户对数据的访问 权限的分类 1. 系统权限:能够存取数据库的权限 2. 对象权限:操作数据库对象的内容 系统权限  1.1 如何创建用户: SQL> create user t ...