题目链接:https://www.luogu.org/problemnew/show/UVA673(洛谷)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=614
题意:给你一个字符串,判断是否合法的括号匹配。
注意输入:
现在掌握的gets的替代有两个:
1、fgets(s,sizeof(s),stdin);
2、cin.getline(s,sizeof(s))
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
getchar();
while(T--)
{
char s[10004];
int f1=1;
fgets(s,sizeof(s),stdin);
/*if(strcmp(s,"\n")==0)
{
cout<<"Yes"<<endl;
continue;
}*/
int len=strlen(s),flag=1;
//printf("%d\n",len);
stack<char>st;
for(int i=0; i<len-1; i++)
{
if(s[i]=='(')
{
st.push(s[i]);
}
else if(s[i]=='[')
{
st.push(s[i]);
}
else if(s[i]==')')
{
if(!(st.empty())&&st.top()=='(')
{
st.pop();
}
else
{
flag=0;
}
}
else if(s[i]==']')
{
if((!st.empty())&&st.top()=='[')
{
st.pop();
}
else
{
flag=0;
}
}else{
flag=0;
}
}
if(flag&&st.size()==0)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}