40分,求大佬帮助!
  • 板块学术版
  • 楼主liyanzhi123
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/7/6 22:06
  • 上次更新2023/11/4 18:31:09
查看原帖
40分,求大佬帮助!
534593
liyanzhi123楼主2021/7/6 22:06

题目网址http://ybt.ssoier.cn:8088/problem_show.php?pid=1355

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int top=0,s[1005],n,ans[100005];
char a[1005];
int panding(int q)
{		
		int len=strlen(a);
		for(int i=0;i<len;i++)
		{
			if(a[i]=='('||a[i]=='['||a[i]=='{'||a[i]=='<')
			  {	
			  	top++;
			  	s[top]=a[i];
			  }
			if(a[i]==')')
			  {
			  	if(s[top]=='(')]
			  	  top--;
			  	else
			  	  return 0;
			  }
			if(a[i]==']')
			  {
			  	if(s[top]=='[')
			  	  top--;
			  	else
			  	  return 0;
			  }
			if(a[i]=='>')
			  {
			  	if(s[top]=='<')
			  	  top--;
			  	else
			  	  return 0;
			  }
			if(a[i]=='}')
			  {
			  	if(s[top]=='{')
			  	  top--;
			  	else
			  	  return 0;
			  }
		}
		if(top==0)
		  return 1;
		else
		  return 0;
}
int main( )
{
	cin>>n;
	for(int q=1;q<=n;q++) 
	{
		memset(s,0,sizeof(s));
		scanf("%s",a);
		ans[q]=panding(q);
	}	
	for(int i=1;i<=n;i++)
	{
		if(ans[i]==1)
		  cout<<"YES"<<endl;
		else
		  cout<<"NO"<<endl;
	}
	return 0;
}
2021/7/6 22:06
加载中...