UVa 673 (括号配对) Parentheses Balance
本来是当做水题来做的,后来发现这道题略坑。
首先输入的字符串可能是空串,所以我用了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的更多相关文章
- 平衡的括号 (Parentheses Balance UVA - 673)
题目描述: 原题:https://vjudge.net/problem/UVA-673 题目思路: 1.水题 2.栈+模拟 3.坑在有空串 AC代码 #include <iostream> ...
- 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 ...
- UVa673 Parentheses Balance
// UVa673 Parentheses Balance // 题意:输入一个包含()和[]的括号序列,判断是否合法. // 具体递归定义如下:1.空串合法:2.如果A和B都合法,则AB合法:3.如 ...
- UVA 673 (13.08.17)
Parentheses Balance You are given a string consisting of parentheses () and []. Astring of this ty ...
- Python学习记录:括号配对检测问题
Python学习记录:括号配对检测问题 一.问题描述 在练习Python程序题的时候,我遇到了括号配对检测问题. 问题描述:提示用户输入一行字符串,其中可能包括小括号 (),请检查小括号是否配对正确, ...
- 上一篇括号配对让人联想起catalan数,顺便转载一篇归纳的还不错的文章
转载请注明来自souldak,微博:@evagle 怎么样才是合法的组合? 只要每一时刻保证左括号的数目>=右括号的数目即可. 直接递归就行,每次递归加一个括号,左括号只要还有就能加,右括号要保 ...
- 括号配对检测 A
括号配对检测 A ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
随机推荐
- ecshop订单中配送方式报错
警告内容:Warning: number_format() expects parameter 1 to be double, string given in D:\wamp\www\ecshop_o ...
- Python 中的引用和类属性的初步理解
最近对Python 的对象引用机制稍微研究了一下,留下笔记,以供查阅. 首先有一点是明确的:「Python 中一切皆对象」. 那么,这到底意味着什么呢? 如下代码: #!/usr/bin/env py ...
- cadence 焊盘制作小结
因为以前一直用altium designer 话PCB,做封装的时候焊盘是不用自己操心的,但是开始用cadence以后发现好多以前不太懂的东西,需要自己画焊盘,这就导致需要了解好多自己以前不懂的东西, ...
- 在fedora 桌面上添加应用程序
在网上下了个android studio,这个程序只是的压缩包,解压后程序也只能在SHELL下敲入studio.sh才能运行 能不能向其他程序一样,在fedora桌面上找到应用程序,点击执行呢.在网上 ...
- 移动端webapp开发必备知识
移动设备的用户越来越多,每天android手机的激活量都已经超过130万台,所以我们面向移动终端的WebAPP也开始跟进了.本文主要介绍webapp的开发与调试的相关知识和经验,以及给出几种可选的解决 ...
- js时间格式的转换
function System_dateInit(value) { if (value != null) { var d = new Date(value); ...
- Object c中的alloc和init问题
从开始学的NSString *name=[[NSString alloc] init] 起,老师教这句话是分配内存空间,一直在用,从来没考虑过它的内部是怎么实现的.今天无意中看到了这一句代码 NSSt ...
- python学习笔记27(python中sys模块的使用)
sys.argv 命令行参数List,第一个元素是程序本身路径 sys.modules.keys() 返回所有已经导入的模块列表 sys.exc_info() 获取当前正在 ...
- 如何在Ubuntu下搭建Android NDK开发环境
1 搭建Android SDK开发环境 参考在在Ubuntu下搭建Android SDK开发环境(图文)首先在Ubuntu下搭建Android SDK开发环境. 2 下载NDK开发包 打开官网: ht ...
- Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...