南邮 OJ 1436 Brackets
2015-08-05 15:48
113 查看
Brackets
时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte总提交 : 144 测试通过 : 35
比赛描述
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.
输入
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.
输出
For each test case, output “Valid” in one line if the expression is valid, or “Invalid” if not.
样例输入
2
{{[[(())]]}}
({[}])
样例输出
Valid
Invalid
题目来源
NUPT ACM 2010 Personal Ranking Contest
#include<iostream> #include<string> #include<stack> using namespace std; bool match(char c1, char c2){ if('('==c1 && ')'==c2 || '['==c1 && ']'==c2 || '{'==c1 && '}'==c2){ return 1; } return 0; } int main(){ int n,i,len; string s; stack<char> cStack; cin>>n; while(n--){ while(!cStack.empty()){ cStack.pop(); } cin>>s; len = s.length(); for(i=0; i<len; i++){ if('('==s[i] || '['==s[i] || '{'==s[i]){ cStack.push(s[i]); }else if(')'==s[i] || ']'==s[i] || '}'==s[i]){ if(!cStack.empty() && match(cStack.top(),s[i]) ){ cStack.pop(); }else{ break; } } } if(i>=len && cStack.empty()){ cout<<"Valid"<<endl; }else{ cout<<"Invalid"<<endl; } } }
相关文章推荐
- java大数练习
- jQuery实现checkbox全选 全不选
- JS中函数
- 【转】如何在CentOS/RHEL中安装基于Web的监控系统 linux-das
- java大数练习
- 通过心理学知识提高问题定位与解决能力(上)
- 转 Linux 基础知识
- C++ list和用vector区别
- 14. Element className 属性
- 南邮 OJ 1434 Fence
- [LeetCode]3Sum
- ios开发日志工具之-deviceconsole
- RTEMS-BBB(六):SD卡分步制作
- Angular Services详解
- ARM指令集
- 初识CPU卡、SAM卡/CPU卡简介、SAM卡简介 【转】
- append, appendTo, after区别(preappend、before与这几个原理相同)
- 第七篇T语言实例开发,文本与程序的几种打开方法(版5.3)
- 集训第四周(高效算法设计)E题 (区间覆盖问题)
- 关联libgdx及其android源代码的方法