站外题求助
  • 板块学术版
  • 楼主cpu_gal
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/7/23 20:26
  • 上次更新2024/7/23 21:23:27
查看原帖
站外题求助
1324770
cpu_gal楼主2024/7/23 20:26

代码:

#include<bits/stdc++.h>
using namespace std;
stack<int> sta;
string s;
int n,t;
bool match(){
	cin>>s;
	for(int i=0;i<s.size();i++)
	{
		if(s[i]=='<')t=1;
		if(s[i]=='(')t=2;
		if(s[i]=='[')t=3;
		if(s[i]=='{')t=4;
		if(s[i]=='}')t=5;
		if(s[i]==']')t=6;
		if(s[i]==')')t=7;
		if(s[i]=='>')t=8;
		if(t>=1 && t<=4){
			if(sta.top()>=t || sta.empty())
			    sta.push(t);
			else
			    return false;
		}
		if(t>=5 && t<=8)
		    if(!sta.empty() && sta.top()+t==9)
		        sta.pop();
		    else
		        return false;
	}
	if(!sta.empty())
	    return false;
	else
	    return true;
}
int main()
{
	cin>>n;
	while(n--)
	{
		while(!sta.empty()) sta.pop();
		if(match())
		    cout<<"YES"<<endl;
		else
		    cout<<"NO"<<endl;
	}
	return 0;
}
2024/7/23 20:26
加载中...