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

首先输入的字符串可能是空串,所以我用了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]set_time_limit — 设置脚本最大执行时间

    (PHP 4, PHP 5) set_time_limit — 设置脚本最大执行时间 说明 void set_time_limit ( int $seconds ) 设置允许脚本运行的时间,单位为秒. ...

  2. dapper 自定义数据库字段和代码中Model字段不一致时候的mapping方法

    namespace YourNamespace { /// <summary> /// Uses the Name value of the ColumnAttribute specifi ...

  3. TImage 的一些操作

    //给 image上写数字. Image1.Picture.Bitmap.Height:= Image1.Height; Image1.Picture.Bitmap.Width:= Image1.Wi ...

  4. oracle创建表空间、用户

    创建表空间 create tablespace '<数据库名>' datafile '<存储路径>' size 500M default storage (initial 20 ...

  5. 【 Quartz】使用 JobListener (任务监听器可实现) 我想在一个任务执行后在执行第二个任务怎么办呢

    http://liuzidong.iteye.com/blog/1147528 Quartz之JobExecutionException 博客分类: Java Quartz quartzjobexec ...

  6. UI元素的相对自适应

    什么是UI元素的相对自适应 UI元素的相对自适应,顾名思义,是指两个UI元素之间保持一种相对的位置不要变化,例如,UI元素A永远处于UI元素B右边的50像素处位置.再比如,一个UI背景框,不论屏幕尺寸 ...

  7. sharepoint One-Time Passwords (windows basic authentication)

    //设计中,未完成 references: http://www.asp.net/web-api/overview/security/basic-authentication http://techn ...

  8. winform保存登录cookie

       在web程序中,我们通常使会使用cookie来保存一些用户状态,或者权限或者你想保存的东西,但是在CS程序中,如果要使用cookie就必须要做些功课了... 最好注意以下几点:   1.使用成员 ...

  9. 【扩展欧几里得】Bzoj 1407: [Noi2002]Savage

    Description Input 第1行为一个整数N(1<=N<=15),即野人的数目.第2行到第N+1每行为三个整数Ci, Pi, Li (1<=Ci,Pi<=100, 0 ...

  10. 团体程序设计天梯赛-练习集L1-015. 跟奥巴马一起画方块

    L1-015. 跟奥巴马一起画方块 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 美国总统奥巴马不仅呼吁所有人都学习编程,甚至 ...