本来是当做水题来做的,后来发现这道题略坑。

首先输入的字符串可能是空串,所以我用了gets函数,紧接着就被scanf("%d", &n)后面的换行符坑掉了。

于是乎再加一句getchar()

 #include <cstdio>
#include <stack>
#include <cstring>
using namespace std; const int maxn = ;
char s[maxn]; bool ok(const char& c1, const char& c2)
{
if(c1 == '(' && c2 == ')') return true;
if(c1 == '[' && c2 == ']') return true;
return false;
} int main()
{
//freopen("in.txt", "r", stdin); int n;
scanf("%d", &n); getchar();
while(n--)
{
gets(s);
stack<char> S;
while(!S.empty()) S.pop();
int l = strlen(s);
if(l % != )
{
puts("No");
continue;
}
bool flag = true;
for(int i = ; i < l; ++i)
{
if(s[i] == '(' || s[i] == '[') S.push(s[i]);
else
{
if(S.empty()) { flag = false; break; }
else if(ok(S.top(), s[i])) S.pop();
else { flag = false; break; }
}
}
if(!S.empty()) flag = false;
printf("%s\n", flag ? "Yes" : "No");
} return ;
}

代码君

UVa 673 (括号配对) Parentheses Balance的更多相关文章

  1. 平衡的括号 (Parentheses Balance UVA - 673)

    题目描述: 原题:https://vjudge.net/problem/UVA-673 题目思路: 1.水题 2.栈+模拟 3.坑在有空串 AC代码 #include <iostream> ...

  2. UVa 673 Parentheses Balance -SilverN

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

  3. UVa 673 Parentheses Balance

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

  4. UVa673 Parentheses Balance

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

  5. UVA 673 (13.08.17)

     Parentheses Balance  You are given a string consisting of parentheses () and []. Astring of this ty ...

  6. Python学习记录:括号配对检测问题

    Python学习记录:括号配对检测问题 一.问题描述 在练习Python程序题的时候,我遇到了括号配对检测问题. 问题描述:提示用户输入一行字符串,其中可能包括小括号 (),请检查小括号是否配对正确, ...

  7. 上一篇括号配对让人联想起catalan数,顺便转载一篇归纳的还不错的文章

    转载请注明来自souldak,微博:@evagle 怎么样才是合法的组合? 只要每一时刻保证左括号的数目>=右括号的数目即可. 直接递归就行,每次递归加一个括号,左括号只要还有就能加,右括号要保 ...

  8. 括号配对检测 A

    括号配对检测 A ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮ ...

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

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

随机推荐

  1. 几个好用的截取字符串的php函数分享

    分享几个好用的PHP 截取字符串函数(支持gb2312和utf-8). 1.截取GB2312字符用的函数 <?php /** **截取中文字符串 * edit by www.jbxue.com ...

  2. Mindjet.MindManager“参数错误”解决办法,适用于9.0、10.0和14.0

    MindManager出14.0版本了,但是在应用个别模板的时候会提示“参数错误”,然后自动关闭. 解决办法:   如果是win7系统,可以进入C:\Users\(用户名) \AppData\Loca ...

  3. SQL Server— 存在检测、建库、 建表、约束、外键、级联删除

    /******************************************************************************** *主题: SQL Server- 存 ...

  4. 同时安装vs2010和VS2012后IEnumerable<ModelClientValidationRule>编译错误

    错误 类型“System.Web.Mvc.ModelClientValidationRule”同时存在于“c:\Program Files (x86)\Microsoft ASP.NET\ASP.NE ...

  5. 【分享】SQL Server优化50法

    虽然查询速度慢的原因很多,但是如果通过一定的优化,也可以使查询问题得到一定程度的解决. 查询速度慢的原因很多,常见如下几种: 没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) I/ ...

  6. hdu 3714 Error Curves(三分)

    http://acm.hdu.edu.cn/showproblem.php?pid=3714 [题意]: 题目意思看了很久很久,简单地说就是给你n个二次函数,定义域为[0,1000], 求x在定义域中 ...

  7. 1036: [ZJOI2008]树的统计Count - BZOJ

    Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...

  8. Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed

    Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed MySQLNonT ...

  9. Makefile教程

    Makefile学习教程: 跟我一起写 Makefile 0 Makefile概述 0.1 关于程序的编译和链接 1 Makefile 介绍 1.1 Makefile的规则 1.2 一个示例 1.3 ...

  10. struts2 标签的使用之二 s:iterator

    struts2的s:iterator 可以遍历 数据栈里面的任何数组,集合等等 以下几个简单的demo:s:iterator 标签有3个属性:    value:被迭代的集合    id   :指定集 ...