uva673 - Parentheses Balance(栈)
题意:1、空串合法。2、若A和B合法,则AB合法。3、若A合法,则(A)和[A]合法。
思路:遍历串,遇到(或[,则压入队列,若遇到),判断:若栈空,则不合法;若栈顶元素不是(,也不合法。]同理。因为判断的是栈顶元素,所以能成对的左括号都可及时弹出。
注意:1、用gets读,因空串合法。2、遍历后,栈不为空,也不合法。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<sstream>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<list>
using namespace std;
const int MAXN=+;
const int INF=0x7f7f7f7f;
const double PI=acos(1.0);
typedef long long ll;
typedef unsigned long long llu;
char s[];
stack<char> sta;
int main()
{
int n;
scanf("%d",&n);
getchar();//= =
while(n--)
{
bool ans=true;
while(!sta.empty())//注意将栈清空
sta.pop();
gets(s);//注意题意,空串合法
for(int i=; s[i]; i++)
{
if(s[i]=='('||s[i]=='[')//(,[,),]别打错= =
sta.push(s[i]);
else if(s[i]==')')
{
if(sta.empty()||sta.top()!='(')
{
ans=false;
break;
}
sta.pop();
}
else if(s[i]==']')
{
if(sta.empty()||sta.top()!='[')//top是栈顶元素
{
ans=false;
break;//= =
}
sta.pop();
}
}
if(!sta.empty()) ans=false;//以上操作会将与之成对的左括号弹出栈,若栈不为空,说明串内有不成对的括号,是不合法的
printf("%s\n",ans ? "Yes" : "No");
}
return ;
}
uva673 - Parentheses Balance(栈)的更多相关文章
- UVa673 Parentheses Balance
// UVa673 Parentheses Balance // 题意:输入一个包含()和[]的括号序列,判断是否合法. // 具体递归定义如下:1.空串合法:2.如果A和B都合法,则AB合法:3.如 ...
- UVA-673 Parentheses Balance(栈)
题目大意:给一个括号串,看是否匹配. 题目分析:一开始用区间DP写的,超时了... 注意:空串合法. 代码如下: # include<iostream> # include<cstd ...
- UVA 673 Parentheses Balance (栈)
题意描述: 给出一段只包含()和[]的字符串,判断是否合法,合法输出YES,不合法输出NO 规则: 1.该串为空,则合法 2.若A合法,B合法,则AB合法 3.若A合法,则(A)和[A]均合法 解题思 ...
- UVa 673 Parentheses Balance -SilverN
You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...
- UVa 673 Parentheses Balance
一个匹配左右括号的问题 /*UVa 673 Parentheses Balance*/ #include<iostream> #include<algorithm> #incl ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
- 栈及其DFS:B - Parentheses Balance
解题心得及总结: 总结: 1.递推:又1推出n,数列中的基本到通项,最终目标得出通项公式. 递归:又n先压缩栈到1,再从函数的出口找到1,又1到n,再从n计算到1: 2.判断是否可以由递推或递推得出, ...
- UVa 673 Parentheses Balance【栈】
题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配, ...
- 【UVA】673 Parentheses Balance(栈处理表达式)
题目 题目 分析 写了个平淡无奇的栈处理表达式,在WA了5发后发现,我没处理空串,,,,(或者说鲁棒性差? 代码 #include <bits/stdc++.h> usin ...
随机推荐
- Codeforces Bubble Cup 8 - Finals [Online Mirror] F. Bulbo DP
F. Bulbo Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/F Des ...
- OpenscenGraph中控制swapbuffer的方法(用于多机大屏幕同步显示机制)
**************************************************************************************************** ...
- exe文件当前目录搜索文件
方法: //std::string dir = "C:\\Users\\xzd\\Documents\\KinectFile\\2014-09-07\\Select\\mengyue\\&q ...
- [Angular 2] Understanding @Injectable
In order to resolve a dependency, Angular’s DI uses type annotations. To make sure these types are p ...
- PHP概率算法(适用于抽奖、随机广告)
做网站类的有时会弄个活动什么的,来让用户参加,既吸引用户注册,又提高网站的用户活跃度.同时参加的用户会获得一定的奖品,有100%中奖的,也有按一定概率中奖的,大的比如中个ipad.iphone5,小的 ...
- windows下的python扩展包下载地址
比如lxml什么的 Unofficial Windows Binaries for Python Extension Packages pip install xxx.whl
- CentOS 6.4 安装 thrift-0.9.3
前言 为了能给.NET的程序提供HBase访问接口需要在Hadoop/Spark集群上安装Thrift Thrift介绍 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强 ...
- Helpers\ReservedWords
Helpers\ReservedWords This helper returns an array of reserved words, this includes php 7's new rese ...
- P6Spy 、 SQL Profiler
P6Spy 在优化Hibernate性能的时候,很重要的一点就是要看到Hibernate底层执行的SQL 虽然通过打印日志配合Hibernate的show_sql属性能够拼凑出Hibernate底层执 ...
- Python_sklearn机器学习库学习笔记(五)k-means(聚类)
# K的选择:肘部法则 如果问题中没有指定 的值,可以通过肘部法则这一技术来估计聚类数量.肘部法则会把不同 值的成本函数值画出来.随着 值的增大,平均畸变程度会减小:每个类包含的样本数会减少,于是样本 ...