题意: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(栈)的更多相关文章

  1. UVa673 Parentheses Balance

    // UVa673 Parentheses Balance // 题意:输入一个包含()和[]的括号序列,判断是否合法. // 具体递归定义如下:1.空串合法:2.如果A和B都合法,则AB合法:3.如 ...

  2. UVA-673 Parentheses Balance(栈)

    题目大意:给一个括号串,看是否匹配. 题目分析:一开始用区间DP写的,超时了... 注意:空串合法. 代码如下: # include<iostream> # include<cstd ...

  3. UVA 673 Parentheses Balance (栈)

    题意描述: 给出一段只包含()和[]的字符串,判断是否合法,合法输出YES,不合法输出NO 规则: 1.该串为空,则合法 2.若A合法,B合法,则AB合法 3.若A合法,则(A)和[A]均合法 解题思 ...

  4. UVa 673 Parentheses Balance -SilverN

    You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...

  5. UVa 673 Parentheses Balance

    一个匹配左右括号的问题 /*UVa 673 Parentheses Balance*/ #include<iostream> #include<algorithm> #incl ...

  6. UVa 673 Parentheses Balance(栈的使用)

     栈 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description You are ...

  7. 栈及其DFS:B - Parentheses Balance

    解题心得及总结: 总结: 1.递推:又1推出n,数列中的基本到通项,最终目标得出通项公式. 递归:又n先压缩栈到1,再从函数的出口找到1,又1到n,再从n计算到1: 2.判断是否可以由递推或递推得出, ...

  8. UVa 673 Parentheses Balance【栈】

    题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配, ...

  9. 【UVA】673 Parentheses Balance(栈处理表达式)

    题目 题目     分析 写了个平淡无奇的栈处理表达式,在WA了5发后发现,我没处理空串,,,,(或者说鲁棒性差?     代码 #include <bits/stdc++.h> usin ...

随机推荐

  1. UVA 11990 ``Dynamic'' Inversion 动态逆序对

    ``Dynamic'' Inversion Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/index ...

  2. Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp

    C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  3. [Angular 2] Understanding OpaqueToken

    When using provider string tokens, there’s a chance they collide with other third-party tokens. Angu ...

  4. Windows下codeblocks的安装与配置

    最近自己正好要使用codeblocks编写C++程序,安装好却发现无法编译,如果您也遇到相似问题,可以参考本文. 如果您已安装codeblocks,想转换成中文界面,直接参考Step 2. 如果您C程 ...

  5. JS_CSS_logon_Mask

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. JSONP(处理跨域问题)

    Ajax直接请求普通文件存在跨域无权限访问的问题 凡是拥有"src"这个属性的标签都拥有跨域的能力,比如<script>.<img>.<iframe& ...

  7. 小白日记43:kali渗透测试之Web渗透-SqlMap自动注入(一)-sqlmap参数详解TARGET

    SqlMap自动注入(一) sqlmap是一款非常强大的开源sql自动化注入工具,可以用来检测和利用sql注入漏洞[动态页面中get/post参数.cookie.HTTP头].它由Python语言开发 ...

  8. Windows Service 之 详解(一)

    一.Windows 服务简介 Windows 服务是可以在系统启动时自动打开的(不需要任何人登录计算机)的程序. 1.适合创建Windows 服务的场景: [1] 在没有用户交互操作的情况下运行程序: ...

  9. jquery 设置style:display 其实很方便的

    ("#id").css('display','none'); $("#id").css('display','block'); 或 $("#id&qu ...

  10. CSS拾遗+技巧集合

    1.实现尖角符号. 这是内联inline-block标签独有的特性. <!DOCTYPE html> <html lang="en"> <head&g ...