【uva-673】 Parentheses Balance(括号匹配问题)
题干:
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
?13
?123([])
(([()])))
([()[]()])()
Sample Output
?123Yes
No
Yes
解题报告:
注意运用栈的知识,再一点就是注意空串!所以这里不能scanf("%s",a);,只能gets(a)或者cin.getline(a , 200 + 5);,因为gets有的地方不让用了!这里编译器选择的是 C++11 5.3.0 还可以用。
ac代码:
#include<iostream>
#include<stack>
#include<cstdio>
#include<cstring>
using namespace std;char a[200+5];
int main()
{int n,len;int flag=0;cin>>n;getchar();while(n--) {flag=0;stack<char > sk;gets(a);len=strlen(a);for(int i = 0; i<len; i++) {if(a[i]=='['||a[i]=='(') {sk.push(a[i]);continue;}if(a[i]==']'&&sk.empty()) {flag=1;break;}if(a[i]==')'&&sk.empty()) {flag=1;break;}if(a[i]==']' && sk.top()!='[') {flag=1;break;}else if(a[i]==']' && sk.top()=='['){//不能直接else 需else if sk.pop(); //错误示范在下面↓↓ }if(a[i]==')' && sk.top()!='(') {flag=1;break;}else if(a[i]==')' && sk.top()=='('){sk.pop();}}if(len==0) {printf("Yes\n");continue;}if(flag||!sk.empty()) {printf("No\n");}else {printf("Yes\n");}} return 0 ;
}// if(a[i]==']' && sk.top()!='[') {
// flag=1;
// break;
// }
// else {
// sk.pop();
// }
// if(a[i]==')' && sk.top()!='(') {
// flag=1;
// break;
// }
// else {
// sk.pop();
// }
法2:(wjh写)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
//D 奇数× ][ [(]) [[((]]))
int cmp(int a,int b)
{return a>b;
}
int main()
{int n;char a[140];cin>>n;getchar(); while(n--){int l1=0,r1=0,l2=0,r2=0;gets(a);int fl1=0,fl2=0,flag=0;int l=strlen(a);for(int i=0;i<l;i++){if(a[i]=='('){l1++;fl1++;}else if(a[i]==')'){r1++;fl1--;}else if(a[i]=='['){l2++;fl2++;}else if(a[i]==']'){r2++;fl2--;}if(fl1<0||fl2<0||a[i]=='('&&a[i+1]==']'||a[i]=='['&&a[i+1]==')')flag=-1;}if(l%2==1)//l&1{printf("No\n");}else if(l1!=r1||l2!=r2){printf("No\n");}else if(flag==-1){printf("No\n");}else printf("Yes\n");}return 0;
}
做题总结:1.看到最近问题,相邻问题 想到栈。
2.正着想不成立时倒着想即可,比如此题:正着想'('不一定和相邻的')'匹配,但是倒着想')'一定和相邻的'('是一对儿。
3.if判断分支的时候,一定弄清楚逻辑结构!那种if里面一个条件的还好,就三种 > 、= 、<,但是这种两个的或者多个的一定分清楚包含关系,对立关系!严格的说两个条件的就有3×3=9 种 可能的情况了!if中!理清楚!
【uva-673】 Parentheses Balance(括号匹配问题)相关推荐
- uva 673 Parentheses Balance
原题: You are given a string consisting of parentheses () and []. A string of this type is said to be ...
- LeetCode20——Valid Parentheses(括号匹配问题,使用栈的知识)
题目: 解法: #include <stack>class Solution { public:bool isValid(string s) {stack<char> pare ...
- LeetCode 堆栈队列 —— 括号匹配(20、232、155)
1. 堆栈(stack) 20,20. Valid Parentheses,括号匹配,堆栈(python 中使用 list 即可实现表示堆栈,list.append:入栈,list.pop():出栈) ...
- Parentheses Balance UVA - 673
Parentheses Balance UVA - 673 在网上看了很多题解,没看到用map做的(可能是我没找到吧).其实用map可以少用很多if语句! #include<cstdio> ...
- UVA 673-Parentheses Balance
UVA 673-Parentheses Balance 题目大意:1 A =[]或()算正确 2 A为BC(B,C都是正确的话)算正确 3 A = [M]或(M)(M为正确的话)算正确 解题思路:用栈 ...
- B - Parentheses Balance (UVA - 673)
- 题目大意 给出两个字符()[],然后根据给的条件来判断. - 解题思路 根据给的三个条件,然后利用栈来处理,对于暂时没有后括号匹配的前括号压入栈,遇到后括号时看栈顶的前括号与其是否匹配,如果匹配则 ...
- UVA - 673 (括号的匹配)
有两种方法: 第一种:数组 思想:观察可匹配成功的字符串可知: 找到第一个i为出括号(')',']')那么与它相匹配的进括号一定是在它左边i-1(最近的) 如果不是第一个出括号,与它相匹配的进括号一定 ...
- Parentheses Balance UVA - 673(模拟)
题目大意:类似于括号匹配. 思路:用栈简单模拟就行了,关键是怎么读入空格. 用getline(cin,str),就行了,如果读入的是空格怎么判断呢,只需要if(str[0]=='\0'),那么为什么不 ...
- 【区间dp】uva10003+ uva 1626 括号匹配问题 【有空自己记忆化写一下!!!】
讲道理,其实我还不是太懂,这个题看到了两种写法 之前大概想的差不多,要这样实现呀: 常规写法,大概n--3 递归写法,稍微好理解一点 好了,接下来自从看了liuchuo的博客我要变身玛丽苏橙色了 题目 ...
- 【艾米莉娅】matrix:valid parentheses括号匹配代码分享(非堆栈版)
之前大风dalao使用堆栈实现了括号匹配,但毕竟堆栈是过于超前的内容,很多小伙伴看不懂.因而分享一下自己做的非堆栈型括号匹配代码. Given a string containing just the ...
最新文章
- 学习笔记:vsphere6 迁移物理机,指定被迁移的IP报错
- MySQL优化之查询缓存(mysql8官方已经废弃这个功能)
- Linux 查看目录常用命令
- Ubuntu时间显示不准确的解决方案
- 摆脱冷气_摆脱匿名类
- dnf机械机器人补丁_干货 | 详解工业机器人控制系统架构
- @RequestBody、@ResponseBody的具体用法和使用时机
- 对'\0'的敬畏——由阶乘想到的
- c++网络编程连接成功后回调onconnected_谈谈网络编程(基于C++)
- vue中watch监听路由传来的参数变化
- java视频教程 设计模式,Gof设计模式视频课程(Java实现)
- Android腾讯微博开放平台入门(三)Oauth授权
- STM32入门开发:编写XPT2046电阻触摸屏驱动(模拟SPI)
- (八)flax Engine游戏引擎物理引擎——物理碰撞器
- Metal 案例03:大批量顶点数据的图形渲染
- ioc和aop全称是什么
- Hadoop配置历史服务器、日志聚集、常用端口号(2.x/3.x)
- MATLAB 像素画绘制APP
- 【pandas drop()和dropna()函数使用详解】
- 信息课为什么不叫计算机课,信息技术课
热门文章
- dart 乘方运算符_Dart系列-运算符
- ajax 批量上传图片插件,TinyMCE多图片批量上传(Ajax)教程
- 计算机软考有学历限制吗,软考中级职称申请积分还需要学历吗?
- 计算机找不到管理无线网络,电脑wifi密码忘了 并且找不到管理无线网络该怎么处理?...
- 切换dns批处理模板
- Asterisk NAT
- opensips简介
- 将Linux下编译的warning警告信息输出到文件中
- 网络编程模型综述 之 UNIX网络I/O模型
- mysql三大范式_MySQL学习笔记