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. java基础之反射

    反射的定义,反射的特性,反射的应用

  2. CentOS 6.8编译安装httpd2.2.31+MySQL5.6.31+PHP5.3.27

    CentOS 6.8编译安装httpd2.2.31+MySQL5.6.31+PHP5.3.27   说明:   操作系统:CentOS 6.8 32位 准备篇: 一.系统约定    软件源代码包存放位 ...

  3. FileShare文件读写锁解决“文件XXX正由另一进程使用,因此该进程无法访问此文件”(转)

    开发过程中,我们往往需要大量与文件交互,读文件,写文件已成家常便饭,本地运行完美,但一上到投产环境,往往会出现很多令人措手不及的意外,或开发中的烦恼,因此,我对普通的C#文件操作做了一次总结,问题大部 ...

  4. "客户端无法连接到远程计算机"错误的解决方法

    问题: 客户端无法连接到远程计算机. 可能没有启用远程连接或者计算机太忙不能接受新的连接. 也可能是网络问题阻止连接.请稍后重新尝试连接. 如果问题仍然存在 请与管理员联系. 解决方法: 1.首先确认 ...

  5. sqlserver2005重新安装(安装汇编错误,安装程序无法连接到数据库服务进行服务配置)

    2014-01-09 16:41 1687人阅读 评论(1) 收藏 举报 分类: 数据库(1) 版权声明:本文为博主原创文章,未经博主允许不得转载. sqlserver2005重新安装(安装汇编错误, ...

  6. Python 学习日记(第二周)

    从这周开始我就正式学习Python 语言了.以后每周都会有一篇有关于学习Python的见闻与大家分享! Python的安装 学习的第一步首先要有一个运行的环境.所以接下来介绍一下安装的步骤. 通过Py ...

  7. Halloween party

    https://www.hackerrank.com/challenges/halloween-party def main(): t = int(raw_input()) for _ in rang ...

  8. 文件:一个任务 - 零基础入门学习Python029

    文件:一个任务 让编程改变世界 Change the world by program 一个任务 这节课,我们需要一起来完成一个任务:将文件(record.txt)中的数据进行分割并按照以下规律保存起 ...

  9. 畅通工程续--hdu1874

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  10. Android平台上使用气压传感器计算海拔高度

    气压传感器两年前已经开始被手机制造商运用在其设备上,但貌似没有引起开发者足够的重视.像Galaxy S III .Galaxy Note 2和小米2手机上都有,不过大家对于气压传感器比较陌生.其实大气 ...