UVA - Parentheses Balance(栈)
发布日期:2021-07-01 00:16:14
浏览次数:2
分类:技术文章
本文共 1395 字,大约阅读时间需要 4 分钟。
题目链接:
Time limit: 3.000 secondsDescription
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.Output
A sequence of ‘Yes’ or ‘No’ on the output file.
Sample Input
3
([]) (([()]))) ([()[]()])()
Sample Output
Yes
No Yes
Problem solving report:
Description: 给你由'[',']','(',')'组成的字符串,问能否进行括号匹配。
Problem solving: 利用栈的性质,只要遇到能匹配的就移出栈,否则就进栈,最后再判断一下栈是否为空就行了。#include#include #include using namespace std;int main(){ int t; char str[130]; scanf("%d%*c", &t); while (t--) { gets(str); stack s; for (int i = 0; str[i]; i++) { if (!s.empty()) { if ((s.top() != '[' || str[i] != ']') && (s.top() != '(' || str[i] != ')')) s.push(str[i]); else s.pop(); } else s.push(str[i]); } if (!s.empty()) printf("No\n"); else printf("Yes\n"); } return 0;}
转载地址:https://lzyws739307453.blog.csdn.net/article/details/86665075 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月14日 16时54分40秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
jqGrid参数列表
2019-05-01
Mybatis 插入与批量插入以及多参数批量删除
2019-05-01
Mybatis实现批量删除操作
2019-05-01
spring中@param和mybatis中@param使用区别
2019-05-01
NOT RECOMMENDED TO RUN AS ROOT
2019-05-01
去掉模态框dialog的滚动条
2019-05-01
固定div位置,不随滚动条上下动
2019-05-01
device eth0 does not seem to present
2019-05-01
【jQuery】判断浏览器类型和版本
2019-05-01
新创建gitlab仓库上传代码不成功解决
2019-05-01
查看linuxmac地址
2019-05-01
git 查看/修改用户名、密码
2019-05-01
jqGrid清空表格中的所有行数据
2019-05-01
jqGrid自定义操作列
2019-05-01
idea 创建dubbo项目
2019-05-01
JAVA_HOME should point to a JDK not a JRE
2019-05-01
IntelliJ IDEA - 热部署插件JRebel 安装使用教程
2019-05-01