求助 RE
查看原帖
求助 RE
278573
liyunze2007楼主2021/10/9 21:24

#include<stack>
using namespace std;
int n;
string t;
int operate(int len,string str)
{
	stack<char>a;
	for(int i=0;i<len;i++)
	{
		if(str[i]!='('&&str[i]!=')'&&str[i]!='['&&str[i]!=']')
			continue;
		if(a.empty()&&(str[i]==']'||str[i]==')'))
		{
			cout<<"No"<<endl;
			return 0;
		}
		if(str[i]=='('||str[i]=='[')
			a.push(str[i]);
		else if(a.empty())
			if((str[i]==')'&&a.top()=='(')||(str[i]==']'&&a.top()=='['))
				a.pop();
			else
			{
				cout<<"No"<<endl;
				return 0;
			}
		else
		{
			cout<<"No"<<endl;
			return 0;
		}
	}
	if(!a.empty())
	{
		cout<<"No"<<endl;
		return 0;
	}
	cout<<"Yes"<<endl;
}
int main()
{
	cin>>n;
	getchar();
	for(int i=1;i<=n;i++)
	{
		getline(cin,t);
		operate(t.length(),t);
	}
	return 0;
}
2021/10/9 21:24
加载中...