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

首先输入的字符串可能是空串,所以我用了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. Notepad++ 运行脚本快捷键设置

    php:cmd /k /path/to/php.exe "$(FULL_CURRENT_PATH)" & ECHO. & PAUSE & EXIT pyth ...

  2. HttpUnit学习笔记

    <!-- htmlUnit --> <dependency> <groupId>net.sourceforge.htmlunit</groupId> & ...

  3. c语言指针说解

    一. 指针定义 1指针的意义 2指针的赋值 指针变量同普通变量一样,使用之前不仅要定义说明, 而且必须赋予具体的值.未经赋值的指针变量不能使用, 否则将造成系统混乱. #include <std ...

  4. jsp与servlet之间的参数传递【转】

    JSP与 servlet之间的传值有两种情况:JSP -> servlet, servlet -> JSP. 通过对象 request和 session (不考虑 application) ...

  5. Ducci Sequence

    Description   A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers ( ...

  6. (转载)linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结

    linux下tar.gz.tar.bz2.zip等解压缩.压缩命令小结 bz2 tgz z等众多压缩文件的压缩与解压方法,需要的朋友可以参考下 1) Linux下最常用的打包程序就是tar了,使用ta ...

  7. EXTJS 4.2 资料 控件之radiogroup 的用法

    最近在EXTJS4.2开发项目,radiogroup的用法,主要是和grid之间的编辑功能:看了好多资料都不对,特此在这里备注记录 代码如下, 1.这是一段Win窗体上的两个单选按钮,设置单选按钮都是 ...

  8. Integer自动装箱分析

    先看看下面的代码会输出什么: public static void main(String[] args) { Integer i = 127; Integer j = 128; Integer ii ...

  9. VS2008的默认打开重置为VS2008

  10. static 内部类

    一般情况下是不可以用static修饰类的.如果一定要用static修饰类的话,通常static修饰的是匿名内部类. 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的(利用static ...