UVa OJ Parentheses Balance 673 括號平衡

UVa OJ Parentheses Balance 673 括號平衡

解題思路:

棧。AC代碼如下,僅供參考。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#define MAXN 128+10

using namespace std;

int main(){
	#ifdef LOCAL
	freopen("673_input.txt","r",stdin);
	freopen("673_output.txt","w",stdout);
	#endif
	string s;
	int n;
	while(scanf("%d\n",&n)==1){
		for(int i=0;i<n;i++){
			getline(cin,s);
			stack<char>p;
			int flag=1;
			for(int j=0;j<s.size();j++){
					if(s[j]=='('||s[j]=='[')
						p.push(s[j]);
					else if(!p.empty()&&s[j]==')'&&p.top()=='(')
						p.pop();
					else if(!p.empty()&&s[j]==']'&&p.top()=='[')
						p.pop();
					else{
						flag=0;
						break;
					}
				}
			printf("%s\n",flag&&p.empty()?"Yes":"No");
		}
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章