题目地址:点击打开链接
C++代码:
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
int n;
cin>>n;//不用while(cin>>n)
cin.get();
while(n--)
{
string s;
stack<char> st;
getline(cin,s);//非得用getline才行,cin还不行,题目中没说有空格啊,坑爹货!
int i,size;
size=s.size();
for(i=0;i<size;++i)
{
if(s[i]=='('||s[i]=='[')
st.push(s[i]);
else
{
if(st.empty()||(s[i]==')'&&st.top()!='(')||(s[i]==']'&&st.top()!='['))
break;
else
st.pop();
}
}
if(i<size||st.size()!=0)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
}
return 0;
}