uva 673 Parentheses Balance
2015-08-09 19:21
483 查看
Parentheses Balanc
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. Your program can assume that the maximum string length is 128.
Input
The file contains a positive integer n and a sequence of n stringsof parentheses () and [], one string a line.
Output
A sequence of Yes or No on the output file.Sample Input
用栈解决,充分利用顺序性。#include<cstdio> #include<iostream> #include<cmath> #include<cstring> #include<climits> #include<string> #include<algorithm> #include<queue> #include<set> #include<vector> #include<map> #include<stack> //typedef __int64 ll; // %I64d char s[135]; using namespace std; stack<char >st; bool match(char x,char y) { if(x=='('&&y==')' ||x=='['&&y==']') return 1; return 0; } int main() { int i,j; int T; scanf("%d",&T); getchar(); while(T--) { gets(s+1); // if(strcmp(s+1,""==0) ) {puts("Yes");continue;} int n=strlen(s+1); while(!st.empty()) st.pop(); for(int i=1;i<=n;i++) { if(st.empty()) { st.push( s[i] ); } else if(!match(st.top(),s[i] ) ) { if( s[i]=='('||s[i]=='[' ) st.push(s[i]); } else { st.pop(); } } puts(st.empty()?"Yes":"No"); } }
相关文章推荐
- iOS开发--UIButton 设置圆角 边框颜色 点击回调方法
- CodeForces 492B
- 一步一步在ubuntu上安装即时通讯服务器-Openfire
- 提取最长转录本的代码
- 理解Scroll Views
- 重复子串问题(五):求最长回文字符子串
- 剑指offer刷题之java实现的二叉搜索树的后序遍历序列
- SYN 攻击 常识 预防
- 重写equals/clone
- 工作习惯-工作时间切勿钻牛角尖耽误进度
- 红黑二叉查找树
- tcp/ip学习笔记
- 【发布】iOS百度贴吧助手1.0
- android广播接收者
- Linux操作系统的常用命令
- java项目转换成javaWeb项目
- LINUX服务器性能评估与优化
- Gas Station
- hibernate 数据库事物并发处理
- 字符串最后一个单词的长度