UVA - 673 - Parentheses Balance(栈)
2015-08-03 15:08
417 查看
UVA 673.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. Your program 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 string a line.Output
A sequence of Yes or No on the output file.Sample Input
3([])
(([()])))
([()])()
Sample Output
YesNo
Yes
题意:括号配对,只有两种括号()和【】,给你一串字符串,判断括号是否配对
题本身并不难,一直卡到我的是这句话“if it is the empty string”,说明在输入里可能会有空行,这就牵扯到用哪个输入问题了,刚开始一直wa,是因为用了scanf,经过多次尝试发现,cin和scanf都会wa,getline和gets就ok了,是因为scanf和cin遇到空格都不处理,这次算是长记性了
[code]#include<cstdio> #include<iostream> #include<algorithm> #include <cstring> #include<string> #include<stack> using namespace std; stack<char>s; int main() { int n; scanf("%d", &n); getchar(); while(n--){ char str[150]; gets(str); //注意输入问题 int len = strlen(str); if(len%2!=0){ printf("No\n"); continue; } int flag = 1; for(int i = 0; i < len; i++){ if(str[i] == '(' || str[i] == '[') s.push(str[i]); else if(!s.empty() && s.top()=='(' && str[i]==')') s.pop(); else if(!s.empty() && s.top()=='[' && str[i]==']') s.pop(); else{ flag = 0; break; } } if(flag&&s.empty()) printf("Yes\n"); else printf("No\n"); while(!s.empty()) s.pop(); //一定要把栈给清空 } return 0; }
相关文章推荐
- POJ 2442 Sequence(堆的应用)
- poj3268
- hibernate学习
- CentOS6.4上安装配置snappy
- mvc项目controller重命名了,用原网页url访问不了了,怎么办?
- 模板的日常~。~。。。(持续更新中...)
- Android--操作图片Exif信息
- 【flash】pv3d制作3d模型查看器
- chap_16 链表
- WPF如何获得自适应缩放设定下的控件的 长和宽
- 调试iOS 已经发布代码 Crash 文件分析出出错对应代码
- angular之directive定义对象说明
- Android Studio 运行真机出现中文乱码
- 回文数(整数数字)
- LNK2005: 函数在两个库中定义
- c++ 覆盖、重载、隐藏
- openssl
- 【面向社区】极客头条使用体验征文(结束)
- TypeScript全纪录
- 15-07-24 HTML--框架集