uva673 - Parentheses Balance(栈)】的更多相关文章

题意:1.空串合法.2.若A和B合法,则AB合法.3.若A合法,则(A)和[A]合法. 思路:遍历串,遇到(或[,则压入队列,若遇到),判断:若栈空,则不合法:若栈顶元素不是(,也不合法.]同理.因为判断的是栈顶元素,所以能成对的左括号都可及时弹出. 注意:1.用gets读,因空串合法.2.遍历后,栈不为空,也不合法. #include<iostream> #include<cstdio> #include<cstring> #include<string>…
// UVa673 Parentheses Balance // 题意:输入一个包含()和[]的括号序列,判断是否合法. // 具体递归定义如下:1.空串合法:2.如果A和B都合法,则AB合法:3.如果A合法则(A)和[A]都合法. // 算法:用一个栈.注意输入可能有空串 #include<cstdio> #include<cstring> #include<iostream> #include<string> #include<algorithm&…
题目大意:给一个括号串,看是否匹配. 题目分析:一开始用区间DP写的,超时了... 注意:空串合法. 代码如下: # include<iostream> # include<cstdio> # include<stack> # include<cstring> # include<algorithm> using namespace std; char p[130]; stack<char>s; bool judge() { int…
题意描述: 给出一段只包含()和[]的字符串,判断是否合法,合法输出YES,不合法输出NO 规则: 1.该串为空,则合法 2.若A合法,B合法,则AB合法 3.若A合法,则(A)和[A]均合法 解题思路: 括号匹配问题,使用栈来解决,需要注意的是用gets来读取,当该串为空时,输出YES 代码实现: #include<stdio.h> #include<string.h> int f(char a[],int l); int main() { ]; int T; scanf(&qu…
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…
一个匹配左右括号的问题 /*UVa 673 Parentheses Balance*/ #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<stack> using namespace std; char c[500]; int n; int pd(char q,char e){ if(q=='(' && e==')')…
 栈 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description 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,…
解题心得及总结: 总结: 1.递推:又1推出n,数列中的基本到通项,最终目标得出通项公式. 递归:又n先压缩栈到1,再从函数的出口找到1,又1到n,再从n计算到1: 2.判断是否可以由递推或递推得出,再判断可以用BFS or DFS得出,BFS使用队列(queue),DFS使用栈(stack). 3.队列,先进先出.如图: 栈先进后出,又称先进后出表. . 例题心得: 1.关键点:队列是否为空,括号是否单项匹配.注意:单项匹配,只能为队列中的左括号和数组中的右括号相互消去.而数列中不能压入右括号…
题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配,如果不匹配,则不合法 还有注意一下每次取出栈顶元素的时候判断栈是否为空,如果为空就要跳出循环 注意空串也是合法的串 #include<iostream> #include<cstdio> #include<cstring> #include <cmath> #i…
题目 题目     分析 写了个平淡无奇的栈处理表达式,在WA了5发后发现,我没处理空串,,,,(或者说鲁棒性差?     代码 #include <bits/stdc++.h> using namespace std; bool equal(char a,char b) { if((a=='(' && b==')') || (a=='[' && b==']')) return true; return false; } int main() { int len…