@lychee123
2017-03-18T11:52:06.000000Z
字数 756
阅读 1462
STL
题意
输入 n,代表有n次输入,每次输入一串只含'(' ')' '[' ']' 四种字符的字符串 允许有空字符串
如果输入为空字符串或者字符串均匹配输出为“Yes”
否则输出“No”
样例
Sample Input
3
([])
(([()])))
([()])()
Sample Output
Yes
No
Yes
分析
运用栈的入栈弹栈,左括号进栈,遇到对应的右括号就弹栈,如果先输入的是右括号也要进栈(之前这里出过点问题)样例 (]) 忘了进右括号就输出了 Yes ,,ԾㅂԾ,,
代码
#include<stdio.h>#include<stack>#include<string>#include<string.h>#include<algorithm>using namespace std;int main(){int n,i;char a[222];scanf("%d",&n);getchar();///吸收输入n的换行符while(n--){gets(a);///可以输入空格if(a[0]==' ')printf("Yes\n");else{stack<char>st;int l=strlen(a);for(i=0;i<l;i++){if(st.empty())st.push(a[i]);else if(a[i]=='('||a[i]=='[')st.push(a[i]);else if(a[i]==')'&&st.top()=='(')st.pop();else if(a[i]==']'&&st.top()=='[')st.pop();elsest.push(a[i]);}if(!st.empty())printf("No\n");elseprintf("Yes\n");}}return 0;}
