swust oj 0116 括号匹配
来源:互联网 发布:java实现二叉树字典 编辑:程序博客网 时间:2024/06/16 21:15
14级卓越班选拔D 15级卓越班选拔D16级卓越班选拔D
题意描述: 在算术表达式中,除了加、减、乘、除等运算外,往往还有括号。包括有大括号{},中括号[],小括号(),尖括号<>等。 对于每一对括号,必须先左边括号,然后右边括号;如果有多个括号,则每种类型的左括号和右括号的个数必须相等;对于多重括号的情形,按运算规则,从外到内的括号嵌套顺序为:大括号->中括号->小括号->尖括号。例如,{[()]},{()},{{}}为一个合法的表达式,而([{}]),{([])},[{<>}]都是非法的。
Description
文件的第一行为一个整数n(1≤n≤100),接下来有n行仅由上述四类括号组成的括号表达式。第i+1行表示第i个表达式。每个括号表达式的长度不超过255。
Input
在输出文件中有N行,其中第I行对应第I个表达式的合法性,合法输出YES,非法输出NO。
Output
1
2
3
4
5
6
5
{[(<>)]}
[()]
<>()[]{}
[{}]
{()}
Sample Input
1
2
3
4
5
6
YES
YES
YES
NO
YES
鬼知道为什么scanf会超时,cin就不会,整整搞了我两天,还以为自己算法有问题==
//by swust_t_p
#include<stdio.h>#include<iostream>#include<algorithm>#include<stack>#include<string.h>using namespace std;#define mem(x,y) memset(x,y,sizeof(x))int main(){int n;cin >> n;while (n--){char a[100];cin >> a;stack<char>b;int f = 1;for (int i = 0; a[i] != 0; i++){// (40 <60 [91 {123if (a[i] == '[' || a[i] == '(' || a[i] == '{' || a[i] == '<'){if (b.empty()){b.push(a[i]);continue;}else if (a[i] > b.top() && a[i] != '<'){f = 0;break;}else if (a[i] == '(' && b.top() == '<'){f = 0; break;}/*else if (a[i] == '<'){b.push(a[i]);}*/else{b.push(a[i]);}}else if (a[i] == ']' || a[i] == ')' || a[i] == '}' || a[i] == '>'){if (b.empty()){f = 0;break;}if (!b.empty() && a[i] == b.top() + 1 || a[i] == b.top() + 2){b.pop();}else{f = 0;break;}}}if (!b.empty()){f = 0;}if (f){cout << "YES" << '\n';}else{cout << "NO" << '\n';}}return 0;}
//by swust_t_p
0 0
- swust oj 0116 括号匹配
- OJ text2 括号匹配问题
- SWUST OJ 1025
- SWUST OJ 1137 STICKS
- SWUST OJ 1804 二进制
- swust OJ 1515
- swust OJ 1619 中位数
- SWUST OJ 2285
- swust.oj 0259
- swust.oj 0964
- swust.oj,0541
- swust.oj.319
- swust.oj.1013
- swust.oj.1015
- swust.oj.1072
- swust.oj.1076
- swust.oj.1075
- swust.oj.1075
- nth-child的使用
- [IOS APP]小豆子幽默丛书
- yum安装之-安装底层软件库--技术支持TPshop商城
- SCU 4440: Rectangle(规律)
- 来北京2年回顾
- swust oj 0116 括号匹配
- python学习心得(1)
- 剑指Offer——面试题8:旋转数组的最小数字
- 2017系统系统分析师的通过人数与通过率探秘
- C++中如何防止类被继承
- Spark ML 2.1 -- Extracting, transforming and selecting features (持续更新)
- CSS表格
- Android:2分钟创建一个好看的关于页面
- 数据结构之图的邻接矩阵的操作