uva 673 Parentheses Balance
来源:互联网 发布:网络电影播放器排行榜 编辑:程序博客网 时间:2024/06/13 13:09
Parentheses Balanc
Parentheses Balanc
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.
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
用栈解决,充分利用顺序性。
#include<cstdio>#include<iostream>#include<cmath>#include<cstring>#include<climits>#include<string>#include<algorithm>#include<queue>#include<set>#include<vector>#include<map>#include<stack>//typedef __int64 ll;// %I64dchar s[135];using namespace std;stack<char >st;bool match(char x,char y){ if(x=='('&&y==')' ||x=='['&&y==']') return 1; return 0;}int main(){ int i,j; int T; scanf("%d",&T); getchar(); while(T--) { gets(s+1);// if(strcmp(s+1,""==0) ) {puts("Yes");continue;} int n=strlen(s+1); while(!st.empty()) st.pop(); for(int i=1;i<=n;i++) { if(st.empty()) { st.push( s[i] ); } else if(!match(st.top(),s[i] ) ) { if( s[i]=='('||s[i]=='[' ) st.push(s[i]); } else { st.pop(); } } puts(st.empty()?"Yes":"No"); }}
0 0
- UVa 673 Parentheses Balance
- uva 673 - Parentheses Balance
- Uva 673 Parentheses Balance
- uva 673 - Parentheses Balance
- UVa 673 Parentheses Balance
- UVa 673 - Parentheses Balance
- UVA 673 Parentheses Balance
- UVa 673 - Parentheses Balance
- UVA 673 Parentheses Balance
- UVa 673 - Parentheses Balance
- Uva-673-Parentheses Balance
- UVa 673 - Parentheses Balance
- uva 673Parentheses Balance
- uva-673 Parentheses Balance
- UVA 673 Parentheses Balance
- UVa 673 - Parentheses Balance
- uva 673 - Parentheses Balance
- uva 673 Parentheses Balance
- B. Berland National Library
- 浅析CVE-2015-3636
- Ubuntu 安装mysql和简单操作
- CS224d Problem set 2作业
- 如何实时查看linux下的日志
- uva 673 Parentheses Balance
- 1078. Hashing (25)
- The Roatin Games poj
- 一笔画问题 NYOJ
- Java字符串题目--删除给定字符串中出现次数最少的字符
- 黑马程序员---description方法
- 103. Binary Tree Zigzag Level Order Traversal
- Palindrome Number
- Cow Exhibition 解题报告