一、题目描述

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. 【Linux驱动】TQ2440 DM9000E网卡驱动移植(Linux-2.6.30.4)

    花了一天的时间研究了一下Linux-2.6.30.4版本号内核下关于TQ2440 DM9000E的网卡驱动移植.总结一下自己的收获. 事实上.在Linux-2.6.30.4版本号内核下有关于网卡驱动, ...

  2. ES 遇到 unassigned shard如何处理?

    解决方法:(1)如果是红色的,可以直接分片shard给你认为有最新(或最多)数据的节点.见下: 摘自:https://discuss.elastic.co/t/how-to-resolve-the-u ...

  3. Fork and Join: Java Can Excel at Painless Parallel Programming Too!---转

    原文地址:http://www.oracle.com/technetwork/articles/java/fork-join-422606.html Multicore processors are ...

  4. VS2012数据绑定控件DataGridView和DataGrid

    在做Windows窗体上ADO.NET数据绑定试验的时候,发现实例中提到的一些控件在vs2012的工具箱中找不到,开始以为是工具箱中的控件太多没看到,结果重新找还是没找到,难道是因为控件升级了?yes ...

  5. MySQL用户添加和分配权限

    mysql数据库insertdelete服务器file mysql> grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口 ...

  6. SPOJ 1029 Matrix Summation【 二维树状数组 】

    题意:二维树状数组,更改值的时候有一点不一样, 是将a[x][y]设置为一个值,所以add的时候要将它和以前的值作差一下 #include<iostream> #include<cs ...

  7. layui中选中select标签 隐藏div

    在select标签中添加 lay-filter="cartype" <script type="text/javascript"> form.on( ...

  8. SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性

    1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...

  9. MyBatis中关于SQL标签的用法(重用SQL 代码段)

    一. 没用sql标签前的SQL映射代码: <select id="findById" resultType="cn.tedu.mybatis.entity.User ...

  10. Webpack的作用(一个基础的打包编译工具在做什么?)

    结论: 转换ES6语法成ES5 处理模块加载依赖 生成一个可以在浏览器加载执行的 js 文件 第一个问题,转换语法,其实我们可以通过babel来做.核心步骤也就是: 通过babylon生成AST 通过 ...