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 ...
随机推荐
- Hbase 学习(一) hbase配置文件同步
最近在狂啃hadoop的书籍,这部<hbase:权威指南>就进入我的视野里面了,啃吧,因为是英文的书籍,有些个人理解不对的地方,欢迎各位拍砖. HDFS和Hbase配置同步 hbase的配 ...
- iOS边练边学--xib文件初使用
一.Xib和storyboard对比 *共同点: 1>都用来描述软件界面 2>都用Interface Builder工具来编辑 3>本质都是转换成代码去创建控件 *不同点 1> ...
- ElasticSearch0910学习
1:es简介 es是一个分布式的搜索引擎,使用java开发,底层使用lucene. 特点:天生支持分布式的.为大数据而生的.基于restful接口. 2:es和solr对比 接口 solr:类似web ...
- BaaS后端即服务 - 概念篇
摘要: 什么是BaaS? BaaS(Backend as a Service)是一种新型的云服务,旨在为移动和Web应用提供后端云服务,包括云端数据/文件存储.账户管理.消息推送.社交媒体整合等.Ba ...
- C# 在Bitmap上绘制文字出现锯齿的问题
解决锯齿问题主要是修改Graphics的属性 修复绘制图片锯齿问题可以修改 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiA ...
- http的GET和POST
本文主要内容 1. GET和POST方法介绍 2. 源代码分析 3. 结果分析 4. 例子参考及引用: http://www.cnblogs.com/zhijianliutang/archiv ...
- matlab下使用svmlib工具包
虽然网络上已经有了很多的类似的东西了吧.但是呢,我自己还是要写一写的. 安装: 对于 libsvm工具包,我们可以去官方网站下载,网址为:http://www.csie.ntu.edu.tw/~cjl ...
- e682. 获得打印页的尺寸
Note that (0, 0) of the Graphics object is at the top-left of the actual page, which is outside the ...
- e645. 处理键盘事件
You can get the key that was pressed either as a key character (which is a Unicode character) or as ...
- jquery -- jquery控制只能输入数字和小数点
控制文本框只能输入数字是一个很常见的需求,比如电话号码的输入.数量的输入等,这时候就需要我们控制文本框只能输入数字.在用js控制之后在英文输入法的状态下去敲击键盘上的非数字键是输不进去的,然而当你转到 ...