uva673 嵌套括号

5 篇文章 0 订阅

题意:紫书

这题比较简单,用一个维护一个栈,遇到左括号入栈,右括号判断栈顶,如果匹配则出栈。

注意:1、输入存在空串 所以要用getline.

  2、此题是嵌套括号而不是括号匹配,因此像[ ( ] ) 输出是no 而不是yes

ac代码:

#include<cstdio>
#include<cstring>
#include<stack>
#include<string>
#include<iostream>
using namespace std;//wa example:[(])
const int maxn=200;
char c[maxn];
int T;

bool solve(){
	stack<char> s;
	for(int i=0;i<strlen(c);i++){
		if(c[i]=='['||c[i]=='(') s.push(c[i]);
		else if(c[i]==']'&&!s.empty()&&s.top()=='[') s.pop();
		else if(c[i]==')'&&!s.empty()&&s.top()=='(') s.pop();
		else return false;
	}
	if(s.empty()) return true;
	return false;
}

int main(){
	//freopen("out.txt","w",stdout);
	while(cin>>T){
		cin.get();
		for(int cas=1;cas<=T;cas++){
			memset(c,0,sizeof(c));
			cin.getline(c,maxn);
			if(solve()) cout<<"Yes"<<endl;
			else cout<<"No"<<endl;
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值