Parentheses Balance uva—673

本来想找个水题涨涨自信呢,但是被折磨得不轻,题目的坑点在与连续输入,会导致回车符也会读入到字符串中 ,换行符也是个字符!!!!

要有针对的方法,可以看到代码里的两种方法,均可以将换行符读入,这样对字符串处理就可以了,如果不读入换行符,那么就会对下一次输入造成影响。

仔细看看代码吧~

#include<cstdio>
#include<cmath>
#include<algorithm> 
#include<iostream>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
char s[150];
//string s;
int main()
{
	int T;
	char str[100];
	scanf("%d",&T);
	fgets(str,100,stdin);
	while(T--)
	{
//		getline(cin,s); //也可以读入换行 
		fgets(s,150,stdin); 
		int n=strlen(s);
		bool error=false;
		stack<char> st;
		for(int i=0;i<n;i++)
		{
			if(s[i]=='(')
			{
				st.push(s[i]);
			}
			else if(s[i]=='[')
			{
				st.push(s[i]);
			}
			else if(s[i]==')')
			{
				if(st.empty()==1)
				{
					error=true;
					break;				
				}			
				else
				{
					if(st.top()=='(')
						st.pop();
					else
					{
						error=true;
						break;
					}
				}
			}
			else if(s[i]==']')
			{
				if(st.empty()==1)
				{
					error=true;
					break;
				}
				else
				{
					if(st.top()=='[')
						st.pop();
					else
					{
						error=true;
						break;
					}
				}
			}
		}
		if(error==true||!st.empty())
			printf("No\n");
		else
			printf("Yes\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值