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 ...
随机推荐
- [DeviceOne开发]-白板的示例
一.简介 该demo通过do_Painterview这个组件实现画板的基本功能,模仿的是Appstore上的叫“白板”的应用,可以更改字体颜色,字体粗细,然后用手指进行绘制,可以回退,清屏,保存到相册 ...
- ie7下<a href="javascript:;">标签不反应
<a href="javascript:;" onclick="functionOne()"> 点击</a> <script&g ...
- CSS常用样式(四)之animation
上篇CSS常用样式(三)这篇博文中已经介绍过了CSS中具有动画效果的transition.transform,今天来大概说说CSS中的animation.animation的加入会使得动画效果更加乐观 ...
- web前端命名规范
在做web项目的时候,命名的规范是很重要.初学者一般急于求成对命名规范没有概念,觉得花时间这些还不如多看几遍框架.其实在我看来,一个良好的命名习惯是很重要的.下面就来介绍一下我总结的命名规范: (1) ...
- SharePoint 2013 表单认证使用ASP.Net配置工具添加用户
前 言 上面一篇博客,我们了解到如何为SharePoint 2013配置表单身份认证,但是添加用户是一个麻烦事儿:其实,我们还可以用Asp.Net的配置工具,为SharePoint 2013添加表单用 ...
- Android View各种尺寸位置相关的方法探究
Android View各种尺寸位置相关的方法探究 本来想做一个View间的碰撞检测之类的. 动手做了才发现不是想象的那么简单. 首先,写好了碰撞检测的工具类如下: package com.mengd ...
- DirectX标准规定 DirectX和OpenGL的不同
DirectX标准规定 DirectX使用左手坐标系. X轴正向指向右,Y轴正向指向上,Z轴正向垂直纸面向内. 编写Direct3D应用程序时,通常只使用4×4的矩阵和1×4的行向量,相乘时行向量在前 ...
- 线程安全、数据同步之 synchronized 与 Lock
本文Demo下载传送门 写在前面 本篇文章讲的东西都是Android开源网络框架NoHttp的核心点,当然线程.多线程.数据安全这是Java中就有的,为了运行快我们用一个Java项目来讲解. 为什么要 ...
- Android提交数据到JavaWeb服务器实现登录
之前学习Android提交数据到php服务器没有成功,在看了两三个星期的视频之后,现在终于实现了与服务器的交互.虽然完成的不是PHP端的,但是在这个过程还是学到了不少东西的.现在我先来展示一下我的成果 ...
- iOS多线程解析
在这篇文章中,我将为你整理一下 iOS 开发中几种多线程方案,以及其使用方法和注意事项.当然也会给出几种多线程的案例,在实际使用中感受它们的区别.还有一点需要说明的是,这篇文章将会使用 Swift 和 ...