一、题目描述

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;
}

转载于:https://www.cnblogs.com/chenximcm/p/6285133.html

SicilyBrackets Matching相关推荐

  1. Xcode couldn‘t find any iOS App Development provisioning profiles matching ‘com.example.***‘

    在更新完iOS14.3后,Xcode真机调试时报错,无法进行真机测试: 报以下错误: No profiles for 'com.example.software.Login' were found: ...

  2. OpenCV中的特征匹配(Feature Matching)

    OpenCV中的特征匹配(Feature Matching) 1. 效果图 2. 原理 3. 源码 3.1 SIFT关键点检测+Knn近邻匹配 3.2 ORB关键点检测+蛮力特征匹配 3.3 SIFT ...

  3. Regular Expression Matching

    正则匹配 Regular Expression Matching Implement regular expression matching with support for '.' and '*'. ...

  4. Git error: Unable to negotiate with X.X.X.X : no matching host key type found . their offer: ssh-dss

    2019独角兽企业重金招聘Python工程师标准>>> I am trying to create a git repository on my web host and clone ...

  5. 【Whalepaper】NLP论文研读 - Keyword-Attentive Deep Semantic Matching

    Whalepaper是由周郴莲负责的一个每周分享论文的活动,带你研读AI领域的论文,快来一起开源学术科研吧! NLP 论文分享:每周日 晚上 九点 CV 论文分享: 每周日 晚上 九点 Res 论文分 ...

  6. DSM: 域不变的立体匹配网络解析(Stereo Matching Networks)

    作者| flow 编辑| 3D视觉开发者社区 导语:本文是由来自牛津大学.百度研究院以及香港中文大学团队发表的论文,该团队提出了域不变的立体匹配网络方法,用于解决立体匹配网络中直接跨域泛化的问题.适合 ...

  7. pandas使用query函数基于判断条件获得dataframe中满足条件的数据行(row)的索引列表(index of rows matching conditions in dataframe)

    pandas使用query函数基于判断条件获得dataframe中满足条件的数据行(row)的索引列表(index of rows matching conditions in dataframe) ...

  8. SAP MM ML81N为采购订单创建服务接收单,报错- No matching PO items selected -

    SAP MM ML81N为采购订单创建服务接收单,报错- No matching PO items selected - SAP里的服务采购流程跟有形的实物采购流程并不相同.除了在采购单据上的差异以外 ...

  9. 【论文阅读记录】Real-Time Correlative Scan Matching

    这篇文章是谷歌的Cartograph中实现real_time_correlative_scan_matcher的论文 Real-Time Correlative Scan Matching Edwin ...

最新文章

  1. 怎样实现两个线程共享一个集合_面试高频考察点:几种线程安全的Map解析
  2. WPF中的Bitmap与byte
  3. boost::subgraph用法的测试程序
  4. java面向对象:关键字 —(17)
  5. Dotnet Core
  6. T-SQL SUM Functions
  7. Spring Boot 介绍、入门
  8. PYTHON解析XML的多种方式效率对比实测
  9. 凉山火灾启示录:面对大火,AI 能做些什么?
  10. Bailian3195 最大公约数【数论】
  11. CDKEY制作:为什么会有CDKEY产生机这样的破解工具?
  12. 【UML】部署图和构件图
  13. PHP第一季视频教程.李炎恢.学习笔记(二)(第2章 基本语法(1))
  14. 锁定计算机密码如何取消,如何取消笔记本电脑硬盘密码锁?
  15. “校内网”将不存在,“校内网”更名为“人人网”
  16. Neokylin-Server离线环境、跨主机、使用Docker部署PXC集群
  17. Windows 11 中打印时提示打印机不兼容,都来是“+”惹的祸
  18. 启动spark- sqI时:Error: A JNI error has occurred, please check your installation and try again Exceptio
  19. “金三银四” 是找工作的最佳时期吗?
  20. Pytorch——报错解决:RuntimeError: Output 0 of SelectBackward is a view and is being modified inplace.

热门文章

  1. neo4j docker
  2. Boosting Xgboost
  3. 02 unix文件系统和命令
  4. Authlib OAuth2.0
  5. Linux多进程的应用
  6. 如何去除计算机的访问限制,访问限制达成,教你如何禁止别人访问你电脑的C盘...
  7. java swing 知乎_java swing 界面开发
  8. 小牛485通讯原理_让你秒懂智能电表工作原理及抄表原理
  9. 阿里分布式数据库服务实践
  10. element table多选表格_【经验总结】vue + element-ui 踩坑—— table 篇