一、题目描述

Let us define a regular brackets sequence in the following way:

  1. Empty sequence is a regular sequence.

  2. If S is a regular sequence, then (S) , [S] and {S} are both regular sequences.

  3. 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的更多相关文章

  1. sicily 1035. DNA matching

    题意:判断基因链是否匹配,匹配的双链数加1,并要标记,下次比较不能重用! 解法: 打擂台法 #include<iostream> #include<string> #inclu ...

  2. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  3. CodeForces 149D Coloring Brackets

    Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

  4. Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp

    题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...

  5. Code Forces 149DColoring Brackets(区间DP)

     Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. 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 ...

  7. Coloring Brackets (区间DP)

    Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a soluti ...

  8. CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)

    1.http://codeforces.com/problemset/problem/149/D 2.题目大意 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色, ...

  9. 学习《Hardware-Efficient Bilateral Filtering for Stereo Matching》一文笔记。

    个人收藏了很多香港大学.香港科技大学以及香港中文大学里专门搞图像研究一些博士的个人网站,一般会不定期的浏览他们的作品,最近在看杨庆雄的网点时,发现他又写了一篇双边滤波的文章,并且配有源代码,于是下载下 ...

随机推荐

  1. MongoDB 系列(二) C# 内嵌元素操作 聚合使用

    "_id" : "639d8a50-7864-458f-9a7d-b72647a3d226","ParentGuid" : "00 ...

  2. MHA+ProxySQL 读写分离高可用

    文档结构如下: 1.ProxySQL说明 ProxySQL是mysql的一款中间件的产品,是灵活的mysql代理层,可以实现读写分离,支持query路由器的功能,支持动态指定sql进行缓存,支持动态加 ...

  3. URAL 1297 后缀数组+线段树

    思路: 论文题--*n 倒过来接上 分奇偶讨论 求LCP 搞棵线段树即可 //By SiriusRen #include <cstdio> #include <cstring> ...

  4. win10+ubuntu的坑

    最近几天考虑了诸多,包括分区大小,使用烧写工具等等. 但是实际动手还是遇到了彩蛋.rufus是知乎的大神推荐的,因为UUI貌似有些版本安装时候有些问题. 而rufus的界面有诸多选项.ubuntu的图 ...

  5. HTML基础——网站信息显示页面

    1.语法和规范 HTML文件都是以.html或者.htm结尾的.建议使用.html结尾. HTML文件分为头部分(<head></head>)和体部分(<body> ...

  6. UVa 10801 Lift Hopping【floyd 】

    题意:给出n个电梯,每个电梯的运行时间,每个电梯只能在相应的楼层停靠,而且没有楼梯,再给出想去的楼层,问从0层能否到达想去的楼层,求到达的最短时间 建图还是没有建出来--- 因为n<100,可以 ...

  7. Intellij IDEA 2018.3激活破解方法(解决key is invalid)

    1.程序安装包: https://download.jetbrains.8686c.com/idea/ideaIU-2018.3.exe 2.破解补丁:http://idea.lanyus.com/j ...

  8. 多任务-进程之PID

    1.进程pid,如何在程序中获取我们的进程号,从而查看当前的进程 # -*- coding:utf-8 -*- from multiprocessing import Process import o ...

  9. centos 7.1安装frees witch

    http://blog.sina.com.cn/s/blog_539d6e0c0102zgvm.html

  10. AWS中国EC2 公网IP登录免pemKEY修改shh 配置文件

    个人使用记录 1:KEY 授权 chmod 400 VPN.pem 2:连接 ssh -i "VPN.pem" ubuntu@ec2-54-183-119-93.us-west-1 ...