E - Parentheses Balance

原创 2018年04月17日 20:52:52

E - Parentheses Balance




You are given a string consisting of parentheses () and []. A string of this type is said to be correct:

(a) if it is the empty string

(b) if A and B are correct, AB is correct,

(c) if A is correct, (A) and [A] is correct.

Write a program that takes a sequence of strings of this type and check their correctness. Yourprogram can assume that the maximum string length is 128.

Input

The file contains a positive integer n and a sequence of n strings of parentheses ‘()’ and ‘[]’, one stringa line.

Output

A sequence of ‘Yes’ or ‘No’ on the output file.

Sample Input

3

([])

(([()])))

([()[]()])()

Sample Output

Yes

No

Yes





代码:

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;

int main(void)
{
	stack<char> p;
	int i,j,n;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		string s1;
		getline(cin,s1);//(a) if it is the empty string
		int len=s1.length();
		for(i=0;i<len;i++)
		{
			if(p.empty())
			{
				p.push(s1.at(i));
				continue;
			}
			if((p.top()=='('&&s1.at(i)==')')||(p.top()=='['&&s1.at(i)==']'))
			{
				p.pop();
				continue;
			}
			else
			p.push(s1.at(i));
		}
		if(p.empty())
		printf("Yes\n");
		else
		printf("No\n");
		while(!p.empty())
		{
			p.pop();
		}
		s1="";
	}
	return 0;
}


 
版权声明: https://blog.csdn.net/nucleare/article/details/79980599

Parentheses Balance——括号匹配

Parentheses Balance  You are given a string consisting of parentheses () and []. A stri...
  • A_Happy_ACMer
  • A_Happy_ACMer
  • 2015-01-23 08:50:43
  • 467

UVA Parentheses Balance

题目如下: Parentheses Balance You are given a string consisting of parentheses () and []. A string ...
  • u013840081
  • u013840081
  • 2014-05-09 19:08:16
  • 913

UVA 673-Parentheses Balance

UVA 673-Parentheses Balance题目大意:1 A =[]或()算正确 2 A为BC(B,C都是正确的话)算正确 3 A = [M]或(M)(M为正确的话)算正确解题思路:用栈存(...
  • ZeroLH00
  • ZeroLH00
  • 2016-07-22 17:13:41
  • 128

Uva - 673 - Parentheses Balance

用栈就搞定了,忘了刚开始会出现 )) ,]] 这种情况,没有加栈非空的条件,结果RE了好几次,智商太拙计。 AC代码: #include #include #include #include...
  • zyq522376829
  • zyq522376829
  • 2015-06-18 19:05:50
  • 437

Parentheses Balance UVA - 673

问题类型:stack,极简主义代码~ 问题链接 03pie’s solution for [UVA-673]: #include using namespace std; int main(...
  • qq_34677945
  • qq_34677945
  • 2017-05-09 14:09:18
  • 162

Uva 673 Parentheses Balance 平衡的括号

You are given a string consisting of parentheses () and []. A string of this type is said to be corr...
  • wb129945130
  • wb129945130
  • 2015-07-22 17:14:12
  • 217

uva673 Parentheses Balance(模拟+栈)

题意:判断给定的括号是否匹配平衡。 思路:用栈来模拟 遇到'(' 或'['的时候入栈, 遇到 ')'或']'跟栈定匹配。匹配成功则栈顶出栈。 最后栈空则括号平衡。陷阱: 小心))))))))...
  • MistKafka
  • MistKafka
  • 2013-07-25 19:13:27
  • 1959

balance parentheses

题目: Given a string with parentheses, return a string with balanced parentheses  by removing the few...
  • u010157717
  • u010157717
  • 2015-03-21 00:29:42
  • 284

Parentheses Balance

 Parentheses Balance Description You are given a string consisting of parentheses () and []...
  • baidu_22079207
  • baidu_22079207
  • 2015-08-11 11:42:41
  • 213

TopCoder SRM 718

除了第一题我用C with STL写的,其他两个是java题解 Problem Statement The city of Byteland has plans to give the cit...
  • baisedeqingting
  • baisedeqingting
  • 2017-07-13 17:40:44
  • 335
收藏助手
不良信息举报
您举报文章:E - Parentheses Balance
举报原因:
原因补充:

(最多只允许输入30个字)