UVA673 Parentheses Balance
2015-08-20 09:05
609 查看
You are given a string consisting of parentheses () and []. A string 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 check their correctness. Your program can assume that the maximum string length is 128.
one string a line.
这一个简答的题竟然提交了五遍真是醉了。
错误的原因
第一空字符串输出yes,注意到这个了,也有判断条件,还是不对,因为输入用的scanf,当输入空行是没反应,最后换成了gets
第二第二个样例老是意外中断,一直没发现问题,最后意识到是当')'或者‘】'多的时候,栈已经空了,此时取不到栈顶元素,所以意外退出
第三自己构造了一些样例不对,最后发现是在每一次的输入前没有清空栈
就是这些问题,
Parentheses Balance |
(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 check their correctness. Your program can assume that the maximum string length is 128.
Input
The file contains a positive integer n and a sequence of n strings of parentheses () and [],one string a line.
Output
A sequence of Yes or No on the output file.Sample Input
3 ([]) (([()]))) ([()[]()])()
Sample Output
Yes No Yes
这一个简答的题竟然提交了五遍真是醉了。
错误的原因
第一空字符串输出yes,注意到这个了,也有判断条件,还是不对,因为输入用的scanf,当输入空行是没反应,最后换成了gets
第二第二个样例老是意外中断,一直没发现问题,最后意识到是当')'或者‘】'多的时候,栈已经空了,此时取不到栈顶元素,所以意外退出
第三自己构造了一些样例不对,最后发现是在每一次的输入前没有清空栈
就是这些问题,
#include <iostream> #include<cstring> #include<cstdio> #include<stack> using namespace std; stack<char> Stack; int main() { int t; scanf("%d",&t); getchar(); while(t--) { char s[150]; gets(s); int len=strlen(s); if(len==0) printf("Yes\n"); else { int flag=0; while(Stack.size()) Stack.pop(); for(int i=0;i<len;i++) { char ch; if(s[i]=='(' || s[i]=='[') Stack.push(s[i]); if(s[i]==')') { if(Stack.size()==0) { flag=1; break; } else { ch=Stack.top(); if(ch=='(') Stack.pop(); else { flag=1; break; } } } if(s[i]==']') { if(Stack.size()==0) { flag=1; break; } else { ch=Stack.top(); if(ch=='[') Stack.pop(); else { flag=1; break; } } } } if(!flag && Stack.size()==0) printf("Yes\n"); else printf("No\n"); } } return 0; }
相关文章推荐
- cmd下过滤文件名称的两种方法
- 过去的数据如何帮助团队改进
- 多态性
- my ideal
- AfxGetMainWnd()为什么返回NULL
- Havok_2014-1-0_Pc_Xs_User_Guide(2.2.1-创建一个Physics2012对象)
- 23种设计模式概要及易懂的例子
- [codevs1287]矩阵乘法
- 安装 redmine遇到的两个问题
- Spring MVC配置事项
- HeidiSQL软件推荐( MySQL 服务器和数据库管理的图形化界面)
- 01.JSP基础语法
- ZXing读写二维码,桌面和手机的不同用法
- Golang 1.5 正式版网盘下载地址
- 用反射机制实现对数据库数据的增、查例子
- hdu5402(构造;)
- scikit-learn:通过Non-negative matrix factorization (NMF or NNMF)实现LSA(隐含语义分析)
- 【POJ 3292】 Semi-prime H-numbers
- php中创建和调用webservice接口示例
- MyEclipse 6.5 代码自动提示功能配置教程