题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=2
代码:
#include<cstdio>
#include<cstdlib>
#include<string>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<ctime>
#include<stack>
using namespace std;
typedef long long ll;
void fun1(char *str)
{
int ans,left,right,left1,right1,len;
left=right=left1=right1=0;
len=strlen(str);
ans=1;
for(int i=0;i<len;i++)
{
if(str[i]=='(')
left++;
else if(str[i]==')')
right++;
else if(str[i]=='[')
left1++;
else
right1++;
}
if(strstr(str,"[)")!=0 || strstr(str,"(]")!=0 || str[0]==')' || str[0]==']')
ans=0;
if(left==right && left1==right1 && ans)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
void fun2()
{
stack<char> st;
char tt;
while(scanf("%c",&tt) && (tt!='\n'))
{
if(st.empty())
{
st.push(tt);
}
else if(tt=='(' || tt=='[')
{
st.push(tt);
}
else
{
if( (tt==')' && st.top()=='(') || (tt==']' && st.top()=='['))
{
st.pop();
}
else
{
st.push(tt);
}
}
}
if(st.empty())
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
int main()
{
int n;
scanf("%d",&n);
getchar();
char str[10090];
while(n--)
{
//方法一: 字符串搜索函数
// gets(str);
// fun1(str);
//方法二 栈模拟
fun2();
}
return 0;
}