UVa 673 Parentheses Balance (stack)
题目描述 : 判断字符串是不是符合正确的表达式形式。
要点 : 考虑字符串为空的时候,用getline输入,每一次判断后如果为No则要清空栈。对称思想。
注意输入格式。
代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
int main()
{
stack<int> s;
int n;
int value[200];
cin>>n;
cin.ignore();
string str;
while(n--)
{
getline(cin , str);
int len = str.size();
if(len == 0)
cout<<"Yes\n";
else
{
for(int i=0; i<len; i++)
{
if(str[i] == '(')
value[i] = 1;
if(str[i] == ')')
value[i] = -1;
if(str[i] == '[')
value[i] = 2;
if(str[i] == ']')
value[i] = -2;
}
for(int i=0; i<len; i++)
{
if(s.empty())
{
s.push(value[i]);
}
else
{
if(s.top() == (-value[i]) && s.top()>0)
s.pop();
else
s.push(value[i]);
}
}
if(s.empty())
cout<<"Yes\n";
else
{
cout<<"No\n";
while(!s.empty())
{
s.pop();
}
}
}
}
return 0;
}
UVa 673 Parentheses Balance (stack)的更多相关文章
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
- 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(栈处理表达式)
题目 题目 分析 写了个平淡无奇的栈处理表达式,在WA了5发后发现,我没处理空串,,,,(或者说鲁棒性差? 代码 #include <bits/stdc++.h> usin ...
- UVa 673 Parentheses Balance【栈】
题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配, ...
- UVA-673 Parentheses Balance(栈)
题目大意:给一个括号串,看是否匹配. 题目分析:一开始用区间DP写的,超时了... 注意:空串合法. 代码如下: # include<iostream> # include<cstd ...
- Balance(Stack)
栈的运用 mooc视频连接 #include <iostream> using namespace std; ]; ; void Push(char c) { ) { Top = ; S[ ...
- UVA 673 Parentheses Balance (栈)
题意描述: 给出一段只包含()和[]的字符串,判断是否合法,合法输出YES,不合法输出NO 规则: 1.该串为空,则合法 2.若A合法,B合法,则AB合法 3.若A合法,则(A)和[A]均合法 解题思 ...
- Java的堆(Heap)和栈(Stack)的区别
Java中的堆(Heap)是一个运行时数据区,用来存放类的对象:栈(Stack)主要存放基本的数据类型(int.char.double等8种基本数据类型)和对象句柄. 例1 int a=5; int ...
随机推荐
- Java-jdbc工具类DBUtils
创建项目: 导入相应jar包: 看上图. JDBCUtil.java获取数据库连接文件: package com.gordon.jdbcutil; import java.io.InputStream ...
- ubuntu下 apache phpmyadmin 的安装和使用
// Apache //Task: Start Apache 2 Server /启动apache服务 # /etc/init.d/apache2 start //or $ sudo /etc/ini ...
- Spark 快速理解
转自:http://blog.csdn.net/colorant/article/details/8255958 ==是什么 == 目标Scope(解决什么问题) 在大规模的特定数据集上的迭代运算或重 ...
- unity3d绘画手册-------灯光之反射及各个参数解释
下面说一下Reflection Probe, 大家都知道:当使用标准着色器时,每一个材质都会具有一定程度的镜面反射(specularity)和金属反射 (metalness)属性,在没有强大的硬件来处 ...
- 在系统中使用read函数读取文件内容
read函数(读取文件) read函数可以读取文件.读取文件指从某一个已打开地文件中,读取一定数量地字符,然后将这些读取的字符放入某一个预存的缓冲区内,供以后使用. 使用格式如下: number = ...
- FusionMap 检测融合基因
定义:融合基因是指两个或者多个基因联合起来,一起转录形成一个转录本: 检测的意义:融合基因可以作为某些疾病的特异分子标记,比如 bcr/abl融合基因存在于95%以上的慢性粒细胞白血病患者中: AML ...
- 【Java面试题】7 构造器Constructor是否可被override?
构造器Constructor不能被继承,因此不能重写Override,但可以被重载Overload. Constructor不能被继承,所以Constructor也就不能被override.每一个类必 ...
- jquery-包裹元素
1.wrap方法 在每个匹配的元素外层包上一个html元素 参数类型说明: 1)html字符串 $('p').wrap('<div></div>'); 传入的html标签也可以 ...
- SharePoint 沙盒无法启动新的解决方案服务的SPUserCodeV4
开发部署时报错: 错误原因:没有启动该服务: 解决方式:打开管理中心—应用程序管理—服务应用程序--管理服务器上的服务,启动该服务即可.
- 学习 TList 类的实现[8]
现在准备建立 Items 数组属性; 在 public 区输入下面代码:property Items[Index: Integer]: Pointer; 执行 Shift+Ctrl+C 后的代码是: ...