Brackets! Brackets!

Brackets! Brackets!
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 698(239 users) Total Accepted: 272(210 users) Rating: Special Judge: No
Description

There are six kinds of brackets: ‘(‘, ‘)’, ‘[‘, ‘]’, ‘{’, ‘}’. dccmx’s girl friend is now learning java programming language, and got mad with brackets! Now give you a string of brackets. Is it valid? For example:
“(([{}]))” is valid, but “([)]” is not.

Input

First line contains an integer T (T<=10): the number of test case.

Next T lines, each contains a string: the input expression consists of brackets.

The length of a string is between 1 and 100.

Output

For each test case, output “Valid” in one line if the expression is valid, or “Invalid” if not.

Sample Input

2

{{[[(())]]}}

({[}])

Sample Output

Valid

Invalid

注意()()这种情况合法。()())这种情况会令flag==0;()(这种情况会令栈为空。

如果出现一个右括号,那么在栈非空的情况下,如果合法必然栈顶元素和这个右括号相匹配。那么在这两个条件都不能满足的条件下flag==0;

#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
	int T;char gq[101];
	cin>>T;
	while(T--)
	{
		stack<char>ls;
		scanf("%s",&gq);
		int flag=1;
		for(int i=0;i<strlen(gq);i++)
		{
			if(gq[i]=='('||gq[i]=='{'||gq[i]=='[')
			ls.push(gq[i]);
		else
		    {
			if(gq[i]==')')
			{
				if(!ls.empty()&&ls.top()=='(')
				{
					ls.pop();
				}
				else
				{
					flag=0;
					break;
				}
			}
			else if(gq[i]=='}')
			{
				if(!ls.empty()&&ls.top()=='{')

				{
					ls.pop();
				}
				else
				{
					flag=0;
					break;
				}
			}
			else if(gq[i]==']')
			{
				if(!ls.empty()&&ls.top()=='[')

				{
					ls.pop();
				}
				else
				{
					flag=0;
					break;
				}
			}
		  }
		}
		if(!ls.empty()||flag==0)
			cout<<"Invalid"<<endl;
		else
			cout<<"Valid"<<endl;
	}
	return 0;
}
时间: 2024-11-25 20:10:05

Brackets! Brackets!的相关文章

web前端开发工具之Brackets

最近看到很多人热议Brackets,这是一款专为前端开发设计的开发工具,界面看起来是很高大上的,而且还有很多特色,并且是开源的哟! 扩展:可以为Brackets安装扩展,增加Brackets的功能.最为热议的扩展莫非那个可以打开psd的了(忘了叫什么名字),其他还有emmet等实用插件. 实时预览:话说之前没有用过可以实时预览的编辑器,用起来挺爽. 以下是复制而来: Brackets 是一款使用 HTML,CSS,JavaScript 创建的开源的针对 Web 开发的编辑器.实时预览,快速编辑,

CF149D. Coloring Brackets[区间DP !]

不知道为什么居中了,先把代码放这 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N=705,MOD=1e9+7; char s[N]; long long n,f[N][N][5][5]; int st[N],top=0,m[N]; void match(){ for(int i=1;i<=

POJ 2955 Brackets (动规)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2999   Accepted: 1536 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a reg

Codeforces 552E Vanya and Brackets(贪心 + 表达式计算)

题目链接 Vanya and Brackets 题目大意是给出一个只由1-9的数.乘号和加号组成的表达式,若要在这个表达式中加上一对括号,求加上括号的表达式的最大值. 我们发现,左括号的位置肯定是最左端或者某个乘号右边,右括号的位置肯定是最右段或者某个乘号左边. 而乘号最多只有15个,那么暴力枚举就可以了. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b);

POJ 2955 Brackets

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6622   Accepted: 3558 Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular

前端开发利器-Brackets IDE

是什么? http://brackets.io/ A modern, open source text editor that understands web design. 现代, 开源的文本编辑器, 最懂得web设计. With focused visual tools and preprocessor support, Brackets is a modern text editor that makes it easy to design in the browser. 专注可视化工具

POJ1141 Brackets Sequence

Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regular

codeforces 552 E Vanya and Brackets

题意是这样,给出一个运算符只有+跟*,数字都在1到9之间的算式,要你加入一对括号,使得算式的结果尽可能的大,保证最多十五个乘号. 很显然,若要让加入的括号能够影响原本运算的结果,必然是要影响乘法,那么加入的这对括号中必然至少有一个跟乘号是相邻的,恰好乘号的数目很小,那么直接枚举括号的位置即可,每次算出当前解更新ans即可. #include<map> #include<string> #include<cstring> #include<cstdio> #i

Winter-2-STL-B Brackets 解题报告及测试数据

Time Limit:2000MS     Memory Limit:65536KB Description Given a string consisting of brackets of two types find its longest substring that is a regular brackets sequence. Input There are mutiple cases in the input file. Each case contains a string con