栈及其DFS:B - Parentheses Balance
解题心得及总结:
总结:
1、递推:又1推出n,数列中的基本到通项,最终目标得出通项公式。
递归:又n先压缩栈到1,再从函数的出口找到1,又1到n,再从n计算到1;
2、判断是否可以由递推或递推得出,再判断可以用BFS or DFS得出,BFS使用队列(queue),DFS使用栈(stack)。
3、队列,先进先出。如图:
栈先进后出,又称先进后出表。
。
例题心得:
1、关键点:队列是否为空,括号是否单项匹配。注意:单项匹配,只能为队列中的左括号和数组中的右括号相互消去。而数列中不能压入右括号,不然直接跳出(可以看作是剪枝)。
2、数列开大一点,汗~~~!
3、这题有坑,输入空格时会输出“Yes”(审题第一个要求)。
4、由于有输入的坑,所以在输入时会纠结,cin,scanf,不能输出空格,只能使用gets(gets可以输入空格和上一个输入的回车),但是在使用gets时会将上一个回车输入导致输入混乱,所以可以在gets前面加一个getchar()将无用的回车去掉。
5、全局变量中的int、char、long long以及结构体中的元素会自动初始化为0。
例题:
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
#include<stdio.h>
#include<stack>
//是c++中的函数,不可以加.h
#include<iostream>
using namespace std;
int main()
{
char aim[140],now,emp;
int i,j,n,c,length;
bool flag = false;
scanf("%d",&n);;
getchar();
while(n--)
{
i = 0;
flag = false;
//判断是否为空格和是否有右括号压入栈中
stack <char> st;
//栈的定义
memset(aim,0,sizeof(aim));
gets(aim);
//注意gets的坑
length = strlen(aim);
for(i=0;i<length;i++)
{
if(st.empty())
//当栈为空的时候只能够压入,不能取出。
{
st.push(aim[i]);
if(st.top() == ' ')
{
printf("Yes\n");
flag = true;
break;
}
}
else
{
if(!st.empty())
{
now = st.top();
if(now == ')' || now == ']')
{
printf("No\n");
flag = true;
break;
}
}
if(st.empty())
continue;
if(now == '(')
{
if(aim[i] == ')')
{
st.pop();
}
else
st.push(aim[i]);
}
if(now == '[')
{
if(aim[i] == ']')
{
st.pop();
}
else
st.push(aim[i]);
}
}
}
if(flag)
continue;
if(st.empty())
printf("Yes\n");
else
printf("No\n");
}
}
栈及其DFS:B - Parentheses Balance的更多相关文章
- 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 ...
- UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...
- Code POJ - 1780(栈模拟dfs)
题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...
- 【栈模拟dfs】Cells UVALive - 3486
题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...
- 队列和 BFS —— 栈和 DFS
队列和 BFS: 广度优先搜索(BFS)的一个常见应用是找出从根结点到目标结点的最短路径. 示例 这里我们提供一个示例来说明如何使用 BFS 来找出根结点 A 和目标结点 G 之间的最短路径. 洞悉 ...
- 百炼3752:走迷宫--栈实现dfs
3752:走迷宫 总时间限制: 1000ms 内存限制: 65536kB 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走.给定一个迷宫,求从左上角走到右下角最 ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
随机推荐
- ElasticSearch之QueryBuilders
1.termsQuery A filer for a field based on several terms matching on any of them 翻译过来:基于以下几个方面对其中任何匹配 ...
- 使用Python开发环境Wing IDE设立项目注意事项
使用Wing IDE的第一步是建立一个项目文件,这样Wing IDE就可以找到并分析源代码,存储工作. Wing IDE会自动以默认的项目进行启动.在本教程中用户也可以使用这个默认项目进行示例操作.如 ...
- Android应用瘦身
转:https://zhuanlan.zhihu.com/p/25465537 瘦身的目的 从目的导向来看,我们是不会无缘无故去做一件事情的,那我们对应用瘦身的目的是为了什么?答案是:提高下载转化率. ...
- meterpreter > ps
meterpreter > ps Process List============ PID PPID Name Arch Session User Path --- ---- ---- ---- ...
- django Q条件
#q条件from django.db.models import Qq = Q(name__startswith="p") | Q(name__startswith="l ...
- shell脚本调试技巧
shell脚本调试之工具——bashdb http://www.cnblogs.com/itcomputer/p/5011845.html
- LeetCode Find Peak Element 找临时最大值
Status: AcceptedRuntime: 9 ms 题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个 ...
- hiho一下 第三十八周 二分答案
题目链接:http://hihocoder.com/contest/hiho38/problem/1 ,挺难想的解题思路,好题. 按照提示的算法来: 我们需要找什么? 在这个题目中我们需要找的是路径最 ...
- 使用Java+SAP云平台+SAP Cloud Connector调用ABAP On-Premise系统里的函数
最近Jerry接到一个原型开发的任务,需要在微信里调用ABAP On Premise系统(SAP CRM On-Premise)里的某些函数.具体场景和我之前的公众号文章 Cloud for Cust ...
- Linux命令技巧:如何在Linux下重命名多个文件
我知道我可以用mv命令重命名文件.但是当我想重命名很多文件怎么办?如果为每个文件都这么做将会是很乏味的.有没有办法一次性重命名多个文件? 在Linux中,当你想要改变一个文件名,使用mv命令就好了.然 ...