UVA 673 Parentheses Balance
题目链接:UVA 673 Parentheses Balance
Runtime Error 运行时错误,越界了。。
栈pop() 前要判断栈是否为空。
字符的输入还是个问题。。。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn = 10010;
const int INF = 0x3f3f3f3f;
char a[maxn];
int main()
{
int t;
cin>>t;
getchar(); //吃掉一个回车
string s;
stack<char> st1,st2;
while(t--)
{
getline(cin,s);
int len = s.length();
while(!st1.empty()) st1.pop();
while(!st2.empty()) st2.pop();
for(int i=0;i<len;i++)
{
if(s[i]=='(')
st1.push(s[i]);
else if(s[i]=='[')
st1.push(s[i]);
else if(s[i]==')')
{
st2.push(s[i]);
if(!st1.empty()&&!st2.empty()&&st1.top()=='(')
{
st1.pop();
st2.pop();
}
}
else if(s[i]==']')
{
st2.push(s[i]);
if(!st1.empty()&&!st2.empty()&&st1.top()=='[')
{
st1.pop();
st2.pop();
}
}
}
if(st1.empty()&&st2.empty())
printf("Yes\n");
else
printf("No\n");
}
return 0;
}