2017年西北工业大学夏令营第五题

#include<iostream>
#include<string>
#include<stack>
using namespace std;
int a[300][300]={0};
int main(){

	int n;
	a['{']['}'] = 1;
	a['['][']'] = 1;
	a['('][')'] = 1;
	string str;
	cin>>n;
	for(int i = 0;i < n;i++){
		cin>>str;
		cout<<str<<endl;
		stack<char>s;
		int j;
		bool flag = true;
		for(j = 0;j < str.length();j++){
			if(str[j] == '{'||str[j] == '['||str[j] == '('){
				s.push(str[j]);//左边负号 
			}else if(str[j] == '}'||str[j] == ']'||str[j] == ')'){
				//右边符号 
				if(s.size() == 0){//栈为空,不匹配 
					cout<<"no"<<endl;
					flag = false;
					break;
				}
				if(a[s.top()][str[j]]==1){//匹配,出栈 
					s.pop();
				}else{//不匹配 
					cout<<"no"<<endl;
					flag = false;
					break;
				}
			}
		}
		if(flag&&s.size()==0){//若匹配,则栈为空,并且flag为true 
			cout<<"yes"<<endl;
		}
		if(flag&&s.size()>0){//若匹配,则栈不为空或者flag为false
			cout<<"no"<<endl;
		}
	}
	return 0;
} 
发布了323 篇原创文章 · 获赞 64 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/PriestessofBirth/article/details/105174281
今日推荐