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.判断是否可以由递推或递推得出, ...
随机推荐
- F - kebab HDU - 2883 (最大流构图)
Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on a long thin stic ...
- c语言中对字段宽度的理解?
/************************************************************************* > File Name: printf.c ...
- PAT甲级——1035 Password (20分)
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...
- centos6.8 yum安装mysql 5.6
一.检查系统是否安装其他版本的MYSQL数据 #yum list installed | grep mysql #yum -y remove mysql-libs.x86_64 二.安装及配置 # w ...
- TPO4-2 Cave Art in Europe
Perhaps, like many contemporary peoples, Upper Paleolithic men and women believed that the drawing o ...
- elementUI的select全选
elementUI中的el-select全选 <template> <el-select class="handle-select" size="min ...
- iOS 自定义只有年月的DatePikerView
头文件: @interface YearMonthPikerView : UIView @property (nonatomic,copy) void(^cancelBlock)(); @proper ...
- 迅为IMX6Q开发板提供原理图_底板PCB_驱动程序源码_芯片和LCD数据手册_开发板环境_使用手册
迅为IMX6开发板: Android4.4/6.0系统 Linux + Qt5.7系统 Ubuntu12.04系统 部分案例:HMI:3D打印机:医疗设备:工控机:触控一体机:车载终端 核心板 ...
- 编译安装 logstash-output-jdbc
环境 mac https://github.com/theangryangel/logstash-output-jdbc logstash-plugin install logstash-output ...
- logback日志大量写磁盘导致微服务不能正常响应的解决方案
最近几天,遇到一个莫名其妙的问题,每天几乎同一时段微服务自己跑着跑着就假死了,过几个小时就又自动恢复了. 通过对定时任务.网卡.内存.磁盘.业务日志的排查分析,只有磁盘的IO在假死前一段时间偏高,经查 ...