<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》一文笔记。
个人收藏了很多香港大学.香港科技大学以及香港中文大学里专门搞图像研究一些博士的个人网站,一般会不定期的浏览他们的作品,最近在看杨庆雄的网点时,发现他又写了一篇双边滤波的文章,并且配有源代码,于是下载下 ...
随机推荐
- HDU 1051: Wooden Sticks(贪心)
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Visual C++文件后缀名释义
[1] .APS:存放二进制资源的资源辅助中间文件(可加快资源装载速度). [2] .BMP:位图资源文件. [3] .BSC:浏览信息文件.由浏览信息维护工具(BSCMAKE)从原始浏览信息文件(. ...
- m_Orchestrate learning system---十、解决bug最根本的操作是什么
m_Orchestrate learning system---十.解决bug最根本的操作是什么 一.总结 一句话总结:多学多练,遇到bug超级轻松 1.如何查看js代码的异常? 开发者选项里面可以查 ...
- VC 下加载 JPG / JPEG / GIF / PNG 图片最简单的方法
VC MFC 提供的 API LoadBitmap / LoadImage 类 CBitmap 等都只能操作 BMP 位图,图标.对于其他常用的 JPG / JPEG / GIF / PNG 格式,它 ...
- ubuntu修改顶栏颜色
title: ubuntu修改顶栏颜色 toc: false date: 2018-09-29 19:14:01 categories: methods tags: Ubuntu 编辑shell主题的 ...
- App.config:配置系统未能初始化的异常
如上图所示:App.config文件是这样配置的,在后台代码”ISchedulerFactory scheduler = new StdSchedulerFactory();“中抛出了异常 经网上查资 ...
- 利用canvas制作简单的logo
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- select选中值,传this
<select onChange = "a(this)"></select> function a(obj){ $(obj).find("opti ...
- CSS3中的transition
W3C标准中对CSS3的transition是这样描述的: CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击,获得焦点,被点击或对元素任何改变中触发, ...
- linux下mysql 查看默认端口号与修改端口号方法
一.查看默认端口号 1.登录mysql [root@localhost ~]# mysql -uroot -pEnter password: 输入数据库密码: 2.使用show global vari ...