Parentheses Balance (括号平衡)---栈
题目链接:https://vjudge.net/contest/171027#problem/E
Yes的输出条件:
1. 空字符串
2.形如()[];
3.形如([])或者[()]
分析:
1.设置一个变量flag,初始值为1 (注意初始化的位置);
2.括号的左半边入栈;
3.若发现括号右半边的时候判断栈顶是否是对应的左半边(是:删除栈顶元素,否:flag=1,不是平衡的括号)!!!在进行这项判断之前一定要判断栈是不是空的,否则会出现错误,任何与栈有关的删除都是;
4.最后还要判断栈是不是空的,不然向((())这样的字符串也会输出“Yes”;
AC代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<stack>
using namespace std;
int main()
{
string a;
int n,flag;
cin>>n;
getchar();
while (n--)
{
flag=;
stack<char>s;
getline(cin,a);
for (int i=;i<a.size();i++)
{
if (a[i]=='('||a[i]=='[')
s.push(a[i]);
else if (a[i]==')')
{
if (!s.empty())
{
if (s.top()=='(')
s.pop();
else
flag=;
}
else
flag=; }
else if (a[i]==']')
{
if (!s.empty())
{
if (s.top()=='[')
s.pop();
else
flag=;
}
else
flag=; }
}
if (!s.empty())
flag=;
if (flag==)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return ;
}
Parentheses Balance (括号平衡)---栈的更多相关文章
- Leetcode 856. Score of Parentheses 括号得分(栈)
Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...
- UVa 673 Parentheses Balance -SilverN
You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...
- UVa673 Parentheses Balance
// UVa673 Parentheses Balance // 题意:输入一个包含()和[]的括号序列,判断是否合法. // 具体递归定义如下:1.空串合法:2.如果A和B都合法,则AB合法:3.如 ...
- UVa 673 Parentheses Balance
一个匹配左右括号的问题 /*UVa 673 Parentheses Balance*/ #include<iostream> #include<algorithm> #incl ...
- UVA-673 括号匹配--栈
如果是一个合法的序列,每对配对的括号的两个字符('(' 和 ')' 或者 '[' 和 ']')一定是相邻的,每次判断下该字符是否有配对即可. 如果配对,将左括号出栈即可.特别注意:空格也是合法的. A ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- 平衡的括号 (Parentheses Balance UVA - 673)
题目描述: 原题:https://vjudge.net/problem/UVA-673 题目思路: 1.水题 2.栈+模拟 3.坑在有空串 AC代码 #include <iostream> ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
- 栈及其DFS:B - Parentheses Balance
解题心得及总结: 总结: 1.递推:又1推出n,数列中的基本到通项,最终目标得出通项公式. 递归:又n先压缩栈到1,再从函数的出口找到1,又1到n,再从n计算到1: 2.判断是否可以由递推或递推得出, ...
随机推荐
- Linux 标准IO库介绍
1.标准IO和文件IO有什么区别? (1).看起来使用时都是函数,但是:标准IO是C库函数,而文件IO是Linux系统的API. (2).C语言库函数是由API封装而来的.库函数内部也是通过调用API ...
- CodeForces 996B World Cup(思维)
https://codeforces.com/problemset/problem/996/B 题意: 圆形球场有n个门,Allen想要进去看比赛.Allen采取以下方案进入球场:开始Allen站在第 ...
- 吴裕雄--天生自然TensorFlow高层封装:Keras-RNN
# 1. 数据预处理. from keras.layers import LSTM from keras.datasets import imdb from keras.models import S ...
- C#Web网站的创建
一.CS与BS的区别 CS软件:需要在客户端安装软件. BS软件:只需要浏览器就能运行,Web网站就是BS软件. 创建过程: 1.文件新建---新建网站----空白网站 2.右击网站项目---添加网页 ...
- C++中free()与delete的区别
1.new/delete是C++的操作符,而malloc/free是C中的函数. 2.new做两件事,一是分配内存,二是调用类的构造函数:同样,delete会调用类的析构函数和释放内存.而malloc ...
- remove_if 的效率测试
#include <iostream> #include <functional> #include <vector> #include <algorithm ...
- \b 是单词边界锚点 word-boundary anchor,一个“\b”匹配一个单词的一端,两个“\b”匹配一个单词的头尾两端
123 $_ = "beforematcha? fter"; 124 if(/\b\w+a\b/){ 125 print "matched: < ...
- VSFTP服务搭建
title date tags layout CentOS6.5 Vsftp服务安装与配置 2018-09-04 Centos6.5服务器搭建 post 1.安装vsftp服务 [root@local ...
- python_数据类型_总结
- memcached_高可用
memcached高可用 一.magent 1.安装 cd /usr/local/mkdir ./magentcd ./magentwget -c http://memagent.googlecode ...