UVA673Parentheses Balance
来源:互联网 发布:海康威视管理端口设置 编辑:程序博客网 时间:2024/06/03 08:39
UVA-673
题意:1)空串为真 , 2)若字符串A为真,则(A) [A]都为真。3)字符串A和B都真,则字符串AB为真。求给出的串是否为真
解题思路:其实就是求括号的匹配问题。写一个栈,遇到 ( 和 [ 进栈,遇到 ) 和 ] 就和栈顶比较,匹配就栈顶出栈。最后栈为空就是匹配,非空就是不匹配。
/************************************************************************* > File Name: UVA-673.cpp > Author: Narsh > > Created Time: 2016年07月16日 星期六 10时10分48秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;int n,m,l,t;string s;char c[600000];int main () { scanf("%d\n",&t); c[0]=' '; while (t--) { getline(cin,s); n=s.length(); s=" "+s; bool tag=true; l=0; for (int i = 1; i <= n; i++) { if (s[i] == '(' || s[i] == '[') { l++; c[l] = s[i]; }else if(l && ((c[l] == '(' && s[i] == ')') || (c[l] == '[' && s[i] == ']'))) l--; else { l++; c[l]=s[i]; } } if (!l) printf("Yes\n"); else printf("No\n"); }}
0 0
- UVA673Parentheses Balance
- Balance
- balance
- Balance
- Parentheses Balance
- poj1837 Balance
- POJ1837 Balance
- Balance(P1837)
- poj1837 - Balance
- poj1837 Balance
- POJ1837--Balance
- poj1837 Balance
- CF17C Balance
- POJ1837 Balance
- POJ1837:Balance
- POJ1837 Balance
- poj1837 Balance
- Emoogle Balance
- C++中使用STL的hashmap
- opencv 截取轮廓中的图像——实现PS中的抠图功能 Opencv extract area circled by contour
- 表达式合法判断
- opencv_关于特征点匹配的数据结构
- C/C++中static关键字详解
- UVA673Parentheses Balance
- Java通过反射获取带参数构造方法并使用
- 一个PHP+Mysql手工注入例子
- Leetcode解题笔记(Linked List)
- mysql如何随机取数据
- jQuery 事件
- uva 10572 Black & White 插头dp
- keepalived 结合mysql 自动切换
- Java(通过反射获取成员变量并使用)