UVa 673 Parentheses Balance【栈】
题意:输入一个包含"()"和"[]"的序列,判断是否合法
用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配,如果不匹配,则不合法
还有注意一下每次取出栈顶元素的时候判断栈是否为空,如果为空就要跳出循环
注意空串也是合法的串
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=;
char t[maxn]; int main(){
int ncase;
scanf("%d",&ncase);
getchar();
while(ncase--){
stack<char> s;
gets(t);
int len=strlen(t);
int flag=;
for(int i=;i<len;i++){
if(t[i]=='('||t[i]=='[') s.push(t[i]); // printf("s.size()=%d\n",s.size());
else if(t[i]==')'){
if(s.size()==) {
flag=;
break;
}
char ch=s.top();s.pop();
if(ch!='(') flag=;
}
else{
if(s.size()==){
flag=;
break;
}
char ch=s.top();s.pop();
if(ch!='[') flag=;
}
if(flag==) break;
} if(s.size()!=) flag=;
if(flag) printf("Yes\n");
else printf("No\n");
}
return ;
}
UVa 673 Parentheses Balance【栈】的更多相关文章
- UVA 673 Parentheses Balance (栈)
题意描述: 给出一段只包含()和[]的字符串,判断是否合法,合法输出YES,不合法输出NO 规则: 1.该串为空,则合法 2.若A合法,B合法,则AB合法 3.若A合法,则(A)和[A]均合法 解题思 ...
- 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 ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
- UVa 673 Parentheses Balance (stack)
题目描述 : 判断字符串是不是符合正确的表达式形式. 要点 : 考虑字符串为空的时候,用getline输入,每一次判断后如果为No则要清空栈.对称思想. 注意输入格式. 代码: #include &l ...
- 【UVA】673 Parentheses Balance(栈处理表达式)
题目 题目 分析 写了个平淡无奇的栈处理表达式,在WA了5发后发现,我没处理空串,,,,(或者说鲁棒性差? 代码 #include <bits/stdc++.h> usin ...
- uva673 - Parentheses Balance(栈)
题意:1.空串合法.2.若A和B合法,则AB合法.3.若A合法,则(A)和[A]合法. 思路:遍历串,遇到(或[,则压入队列,若遇到),判断:若栈空,则不合法:若栈顶元素不是(,也不合法.]同理.因为 ...
- 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 ...
随机推荐
- 谈谈arm下的函数栈
引言 这篇文章简要说说函数是怎么传入参数的,我们都知道,当一个函数调用使用少量参数(ARM上是少于等于4个)时,参数是通过寄存器进行传值(ARM上是通过r0,r1,r2,r3),而当参数多于4个时,会 ...
- 异步JS:$.Deferred的使用
异步JS:$.Deferred的使用 原文链接:http://www.html5rocks.com/en/tutorials/async/deferred/ 当我们构建一个平稳的,响应式的HTML5应 ...
- HDU4776 Ants(Trie && xor)
之前mark下来的一道题,今天填一下坑. 题意是这样子的.给你一棵边上有权的树.然后有树上两点(u,v)的路径有n*(n-1)条,路径(u,v)的权值是边权的xor. 然后下面有m个询问,询问你n*( ...
- (转)Maximum subarray problem--Kadane’s Algorithm
转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
- HDU 1978 How many ways(动态规划)
How many ways http://acm.hdu.edu.cn/showproblem.php?pid=1978 Problem Description 这是一个简单的生存游戏,你控制一个机器 ...
- PCA和LDA
一.PCA 在讲PCA之前,首先有人要问了,为什么我们要使用PCA,PCA到底是干什么的?这里先做一个小小的解释,举个例子:在人脸识别工作中一张人脸图像是60*60=3600维,要处理这样的数 ...
- kafka配置
官网:http://kafka.apache.org/ 主要有3种安装方式: 1. 单机单broker 2. 单机多broker 3. 多机多broker 1. wget http://mirror. ...
- Jenkins+Maven+Git CI环境搭建手册
Jenkins+Maven+Git CI环境搭建手册 环境: OS:Linux version 2.6.32-220.23.2.ali878.el6.x86_64 (ads@kbuild) (gcc ...
- Java-马士兵设计模式学习笔记-建造者模式
一.概述 二.代码 1.Animal.java public interface Animal { public void bark(); } 2.Dog.java public class Dog ...
- 一起简单写一下AIDL,入个门
前话 最近接触了Android开发的一个新知识,AIDL(¬_¬因为到现在都没用过) 因此不断谷歌找资料找Demo,自己尝试写一下. 因为用AndroidStudio作为开发环境,期间遇到过许多问题, ...