UVa 673 Parentheses Balance -SilverN
You are given a string consisting of parentheses () and []. A string of this type is said to be correct:
- (a)
- if it is the empty string
- (b)
- if A and B are correct, AB is correct,
- (c)
- if A is correct, (A) and [A] is correct.
Write a program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.
Input
The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.
Output
A sequence of Yes or No on the output file.
Sample Input
3
([])
(([()])))
([()[]()])()
Sample Output
Yes
No
Yes 用栈模拟匹配括号
/*UVa 673 Parentheses Balance*/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
using namespace std;
char c[];
int n;
int pd(char q,char e){//匹配
if(q=='(' && e==')')return ;
if(q=='[' && e==']')return ;
return ;
}
int main(){
scanf("%d\n",&n);
while(n--){
gets(c);//因为可能读入空串,所以用gets
if(strcmp(c,"\n")==){//空串合法
printf("Yes");
continue;
}
int flag=;
stack<char>st;
int L=strlen(c);
for(int i=;i<L;i++){
if(c[i]=='(' || c[i]=='[') st.push(c[i]);//左括号入栈
else{//右括号处理
if(st.empty()){
flag=;
break;
}
if(pd(st.top(),c[i])==) st.pop();
else{
flag=;
break;
}
}
}
if(flag== || !st.empty())printf("No\n");
else printf("Yes\n");
}
return ;
}
UVa 673 Parentheses Balance -SilverN的更多相关文章
- UVa 673 Parentheses Balance
一个匹配左右括号的问题 /*UVa 673 Parentheses Balance*/ #include<iostream> #include<algorithm> #incl ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
- UVa 673 Parentheses Balance【栈】
题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配, ...
- UVA 673 Parentheses Balance (栈)
题意描述: 给出一段只包含()和[]的字符串,判断是否合法,合法输出YES,不合法输出NO 规则: 1.该串为空,则合法 2.若A合法,B合法,则AB合法 3.若A合法,则(A)和[A]均合法 解题思 ...
- UVa 673 Parentheses Balance (stack)
题目描述 : 判断字符串是不是符合正确的表达式形式. 要点 : 考虑字符串为空的时候,用getline输入,每一次判断后如果为No则要清空栈.对称思想. 注意输入格式. 代码: #include &l ...
- 【UVA】673 Parentheses Balance(栈处理表达式)
题目 题目 分析 写了个平淡无奇的栈处理表达式,在WA了5发后发现,我没处理空串,,,,(或者说鲁棒性差? 代码 #include <bits/stdc++.h> usin ...
- 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 ...
- Parentheses Balance UVA - 673
You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...
随机推荐
- Space.js – HTML 驱动的页面 3D 滚动效果
为了让我们的信息能够有效地沟通,我们需要创建用户和我们的媒体之间的强有力的联系.今天我们就来探讨在网络上呈现故事的新方法,并为此创造了一个开源和免费使用的 JavaScript 库称为 space.j ...
- about reg
var getR=function(str,reg){ return str.match(reg).join(""); } var a=getR("138888" ...
- 【javascript激增的思考04】MVC与Backbone.js(beta)
前言 最近整理了很多前端面试题的东西,今天又去参加了一次面试,不知各位烦不烦,我反正有点累了,于是我们今天继续回到我们前段时间研究的问题,我们再来看看MVC吧. 什么是MVC 又回到这个问题了,到底什 ...
- [deviceone开发]-QQ分享、微信分享和新浪微博分享
一.简介 该demo主要实现QQ分享.微信分享和新浪微博分享.(调试包请到论坛扫描对应二维码下载) 二.效果图 三.相关讨论 http://bbs.deviceone.net/forum.php?mo ...
- gulp-babel 取消严格模式方法
最近项目决定用ES6语法重构,于是引入了gulp-babel去编译ES6. 问题来了,babel编译ES6会自动添加"use strict"在js文件的最前面,这就导致之前的项目文 ...
- 转:与Microsoft Visual Studio集成良好的第三方工具
我们都知道,Microsoft Visual Studio(简称VS)是微软开发的一个非常高效的集成开发环境,但即使已经发展到VS2012,仍然有许多不尽人意的地方.一般来讲,一个项目中需要有控件.代 ...
- 【CoreData】多个数据库使用
在实际开发中,往往需要每个模块使用不同数据库,而CoreData也具备这样的功能,使用起来也很方便: 首先我们创建2个模型文件(School和Educationist) // 1.创建模型文件 (相当 ...
- Android 下拉列表框、文本框、菜单
1.下拉列表框(Spinner) 项目布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andr ...
- 如何把自己的MaC本变成一台服务器
Mac本Xcode10.9以后都默认安装阿帕奇服务器的,所以我们只需要找到他进行配置就可以了 下面看具体操作命令 vim编辑器命令 1.打开apache服务器,sudo apachectl start ...
- iOS 学习 - 10下载(1) NSURLConnection 篇
程序的实现需要借助几个对象: NSURLRequest:建立了一个请求,可以指定缓存策略.超时时间.和NSURLRequest对应的还有一个NSMutableURLRequest,如果请求定义为NSM ...