题目链接:Parentheses Balance UVA - 673
思路:栈的应用。。。。错都在回车!!!
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<iomanip>
#define ll long long
using namespace std;
const int maxn = 100 + 5;
int main()
{
string s;
int t;
scanf("%d",&t);
getchar(); //吃掉回车。。。。
while(t--) {
getline(cin, s);
stack<char> c;
bool f = true;
if(s.length() == 0) {
puts("Yes");
continue;
}
for(int i = 0; i < s.length(); i++) {
if(s[i] == '('||s[i] == '[') c.push(s[i]);
if(s[i] == ')') {
if(c.empty()) { f = false; break;}
else if(c.top() == '(') c.pop();
else { f = false; break;}
}
if(s[i] == ']') {
if(c.empty()) { f = false; break;}
else if(c.top() == '[') c.pop();
else { f = false; break;}
}
}
if(!c.empty() || !f) puts("No");
else puts("Yes");
}
return 0;
}