<Sicily>Brackets Matching
一、题目描述
Let us define a regular brackets sequence in the following way:
Empty sequence is a regular sequence.
If S is a regular sequence, then (S) , [S] and {S} are both regular sequences.
If A and B are regular sequences, then AB is a regular sequence.
For example, all of the following sequences of characters are regular brackets sequences:
(), [], {}, (){[]}
While the following character sequences are not:
(, [, {, )(, ([)], {[(]
Write a program to judge whether a given sequence is a regular bracket sequence or not.
二、输入
The input may contain several test cases.
The first line of each test case contains the length of the bracket sequence N (1<=N<=100). The second line contains N characters including ‘(‘, ‘)’, ‘[‘, ‘]’,’{‘ or ’}’.
Input is terminated by EOF.
三、输出
For each test case, if the sequence is a regular brackets sequence, output “YES” on a single line, else output “NO”.
例如:
输入:
2
()
4
((]]
6
{[]}()
输出:
YES
NO
YES
四、解题思路
这个使用栈来处理,当栈顶不为空的时候,栈顶跟输入的字符(括号)匹配,是否为一对(栈顶的为左边括号,新输入的为右边括号)。若能匹配,pop出栈顶,继续新的输入,否则新输入的字符入栈。
五、代码
#include<iostream>
#include<stack>
#include <stdio.h>
using namespace std;
int main()
{
int leng;
while(cin >> leng)
{
stack<char> strStack;
for(int i = 0; i < leng; i++)
{
char nowChar;
cin >> nowChar;
if(strStack.empty()){strStack.push(nowChar); continue;} //如果栈为空,直接入栈,不需要判断
if(nowChar == '(' || nowChar == '[' || nowChar == '{'){strStack.push(nowChar); continue;} //如果是左边的括号,直接入栈
if(nowChar == ')' && strStack.top() == '('){strStack.pop();} //“()”匹配成功
else if(nowChar == ']' && strStack.top() == '['){strStack.pop();} //“[]”匹配成功
else if(nowChar == '}' && strStack.top() == '{') {strStack.pop();} //“{}”匹配成功
else {strStack.push(nowChar);} //匹配不成功,入栈
}
if(strStack.empty()) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
<Sicily>Brackets Matching的更多相关文章
- sicily 1035. DNA matching
题意:判断基因链是否匹配,匹配的双链数加1,并要标记,下次比较不能重用! 解法: 打擂台法 #include<iostream> #include<string> #inclu ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- CodeForces 149D Coloring Brackets
Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...
- Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp
题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...
- Code Forces 149DColoring Brackets(区间DP)
Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #106 (Div. 2) D. Coloring Brackets —— 区间DP
题目链接:https://vjudge.net/problem/CodeForces-149D D. Coloring Brackets time limit per test 2 seconds m ...
- Coloring Brackets (区间DP)
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a soluti ...
- CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)
1.http://codeforces.com/problemset/problem/149/D 2.题目大意 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色, ...
- 学习《Hardware-Efficient Bilateral Filtering for Stereo Matching》一文笔记。
个人收藏了很多香港大学.香港科技大学以及香港中文大学里专门搞图像研究一些博士的个人网站,一般会不定期的浏览他们的作品,最近在看杨庆雄的网点时,发现他又写了一篇双边滤波的文章,并且配有源代码,于是下载下 ...
随机推荐
- 如何获取Assets的路径
有两种方法可以获取assets的绝对路径: 第一种方法: String path = file:///android_asset/文件名; 第二种方法: InputStream abpath = ge ...
- OSGi 和 C++
2011年 9月我参加了OSGi社区在达姆施塔特的会议,并且有机会与其他与会者探讨本机c++实现的OSGi规范的现状.在这一事件之前我也一直想写一篇博客,来描述关于当前实现OSGi规范的现状和努力—— ...
- 图论之tarjan缩点
缩点,就是把一张有向有环图中的环缩成一个个点,形成一个有向无环图. 首先我介绍一下为什么这题要缩点(有人肯定觉得这是放屁,这不就是缩点的模板题吗?但我们不能这么想,考试的时候不会有人告诉你打什么板上去 ...
- (七)日志采集工具sleuth--分布式链路跟踪(zipkin)
微服务架构上通过业务来划分服务的,通过REST调用,对外暴露的一个接口,可能需要很多个服务协同才能完成这个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败.随着业务的不断 ...
- luogu 2679 子串
子串 史上最简短的一篇博客,毕竟看题解ac心疼我的kmp /* f[i][j][k][0/1]表示A的前i个,B的前j个,用到了k个子串,当前字符选或者不选. 所以f[0][0][0][0]的方案数为 ...
- SVN Commit报错 svn: E155037: Previous operation has not finished; run 'cleanup' if it was interrupted
svn commit 文件出错 svn: E155037: Commit failed (details follow): svn: E155037: Previous operation has n ...
- DotNetCore.1.0.1-VS2015Tools.Preview2.0.3 相关问题及解决办法
本月16号,MS发布了 .NET Core 1.1.作为一个用贯MS产品的小盆友,我第一时间就把相关的安装包下载下来了,然后果断安装(入坑). 我猜你来看这篇博客可能遇到了和我一样的问题. 问题0:正 ...
- 系统出现0x0000006B蓝屏修复,系统文件损坏 bootcat.cache、driver.stl
系统蓝屏,无论如何都不能进入系统,所以你需要一个U盘启动器,就是能绕过电脑的系统进入电脑,可以用U盘做一个U盘启动器,或者其他方法均可以,只要能进入到你的电脑访问C盘即可 2 下载链接内的文件解压后放 ...
- java中,length,length(),size()区别
length——数组的属性: length()——String的方法: size()——集合的方法:
- 强化学习(3)-----DQN
看这篇https://blog.csdn.net/qq_16234613/article/details/80268564 1.DQN 原因:在普通的Q-learning中,当状态和动作空间是离散且维 ...