nyoj--2--括号配对问题(栈函数)
括号配对问题
时间限制:3000 ms  |  内存限制:65535 KB
难度:3
- 描述
 - 现在,有一行括号序列,请你检查这行括号是否配对。
 
#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
using namespace std;
char a[101100];
int main()
{
	int t;
	scanf("%d",&t);
	getchar();
	while(t--)
	{
		scanf("%s",a);
		int l=strlen(a);
		if(l%2==1)
		printf("No\n");
		else
		{
			if(a[0]==']'||a[0]==')')
			printf("No\n");
			else
			{
				stack<char>s;
				for(int i=0;i<l;i++)
				{
					if(s.size()==0)
					s.push(a[i]);
					else
					{
						if(s.top()=='['&&a[i]==']'||s.top()=='('&&a[i]==')')
						s.pop();
						else
						s.push(a[i]);
					}
				}
				if(!s.empty())
				printf("No\n");
				else
				printf("Yes\n");
			}
		}
	}
	return 0;
}
                    
                
                
            
        
浙公网安备 33010602011771号