#include <stack>
#include <string>
using namespace std
int main()
{
int N
cin>>N
while(N--)
{
string a
stack<char> s
cin>>a
for(int i=0i<a.size()i++)
{
if(a[i]=='('||a[i]=='[')
s.push(a[i])
else if(a[i]==')')
{
if(!s.empty()&&s.top()=='(')
s.pop()
else s.push(a[i])
}
else if(a[i]==']')
{
if(!s.empty()&&s.top()=='[')
s.pop()
else s.push(a[i])
}
}
if(s.empty())
cout<<"Yes"<<endl
else
cout<<"No"<<endl
}
return 0
}
ACM的题吧,简单。我这个程序可以判断两种括号的匹配。你这个才一种
#include<stdio.h>int main()
{
int n,j,i
scanf("%d",&n)
for(n>0n--)
{
char s[1000]={0}//不超过1000表示最示最大1000个字符,要给\0留个位置
scanf("%s",s)
for(i=0s[i]i++)
{
if(s[i]==')')
{
j--
if ( j<0 ) //遇到)就要看前面是否有过( ,没有过,则说明不匹配了
break
}
else
if(s[i]=='(')
j++
}
if(j==0)printf("Yes\n")
else
printf("No\n")
}
return 0
}