Parentheses Balance UVA - 673
You are given a string consisting of parentheses ()
and []
. A string of this type is said to be correct:
- (a) if it is the empty string
- (b) if A and B are correct, AB is correct,
- (c) if A is correct,
(
A)
and[
A]
is correct.
Write a program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.
Input
The file contains a positive integer n and a sequence of n strings of parentheses ()
and []
, one string a line. Note that the string can contain arbitrary spaces and it can even be empty.
Output
A sequence of Yes
or No
on the output file.
Sample Input
3
([])
(([()])))
([()[]()])()
Sample Output
Yes
No
Yes
HINT
栈操作
Accepted
#include<bits/stdc++.h>
using namespace std;
int main() {
ofstream fcout;
fcout.open("temp.txt");
int sum, flag = 0;
string s;
cin >> sum;getchar();
while (sum--) {
stack<char>S;
flag = 0;
getline(cin, s);
for (int i = 0;i < s.length();i++) {
if (s[i] == '['|| s[i] == '(')S.push(s[i]);
else if (s[i] == ')')
if (!S.empty() && S.top() == '(')S.pop();
else { flag = 1;break; }
else if (s[i] == ']')
if (!S.empty() && S.top() == '[')S.pop();
else { flag = 1;break; }
}
cout << (S.empty() && !flag ? "Yes" : "No") << endl;
}
}
Parentheses Balance UVA - 673的更多相关文章
- 平衡的括号 (Parentheses Balance UVA - 673)
题目描述: 原题:https://vjudge.net/problem/UVA-673 题目思路: 1.水题 2.栈+模拟 3.坑在有空串 AC代码 #include <iostream> ...
- 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 ...
- 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 ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
- UVa 673 Parentheses Balance【栈】
题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配, ...
- UVa 673 (括号配对) Parentheses Balance
本来是当做水题来做的,后来发现这道题略坑. 首先输入的字符串可能是空串,所以我用了gets函数,紧接着就被scanf("%d", &n)后面的换行符坑掉了. 于是乎再加一句 ...
- UVA 673 Parentheses Balance (栈)
题意描述: 给出一段只包含()和[]的字符串,判断是否合法,合法输出YES,不合法输出NO 规则: 1.该串为空,则合法 2.若A合法,B合法,则AB合法 3.若A合法,则(A)和[A]均合法 解题思 ...
随机推荐
- Ribbon实现负载均衡时 服务提供者注册失败 原因之一
问题描述:提供者注册时总是不用自己配置的server.port 在project structuree中的模块总是自动添加别的模块的文件 那么一定要看看project structure中的langu ...
- 第50天学习打卡(CSS 圆角边框 盒子阴影 定位)
4.4圆角边框 圆角边框: <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- 看完我的笔记不懂也会懂----ECMAscript 567
目录 ECMAscript 567 严格模式 字符串扩展 数值的扩展 Object对象方法扩展 数组的扩展 数组方法的扩展 bind.call.apply用法详解 let const 变量的解构赋值 ...
- 002-LED闪烁
LED闪烁 功能:控制LED模块的小灯闪烁 #include<reg52.h> // 头文件 sbit LED = P2^0; // LED接低电平 void main() //主函数 { ...
- Kubernetes Container lifecycle hooks
简介 在kubernetes中,容器hooks旨在解决服务进程启动与停止时的一些优雅操作需求.比如说进程停止时要给进程一个清理数据的时间,保证服务的请求正常结束,而不是强行中断服务的运行,这样在生产环 ...
- 剑指 Offer 51. 数组中的逆序对 + 归并排序 + 树状数组
剑指 Offer 51. 数组中的逆序对 Offer_51 题目描述 方法一:暴力法(双层循环,超时) package com.walegarrett.offer; /** * @Author Wal ...
- SHELL编程入门简介
一.SHELL软件概念和应用场景 1) 学习Linux技术,不是为了学习系统安装.命令操作.用户权限.配置IP.网络管理,学习Linux技术重点:基于Linux系统部署和维护各种应用软件.程序(Apa ...
- 几大BSD 区别
OpenBSD 侧重于安全,软件包较少,较陈旧,比如 KDE 才 3.5,为了安全舍弃了 sudo 和 linux 兼容层: FreeBSD 是开发者最多用户最多软件包最多的,有 ZFS 和 Linu ...
- url里bookmark是什么意思
<a rel="bookmark" href="abc.com"> 点击查看 </a> rel 这个属性的全称是 relationsh ...
- 用实战玩转pandas数据分析(一)——用户消费行为分析(python)
CD商品订单数据的分析总结.根据订单数据(用户的消费记录),从时间维度和用户维度,分析该网站用户的消费行为.通过此案例,总结订单数据的一些共性,能通过用户的消费记录挖掘出对业务有用的信息.对其他产 ...