C. Bad Sequence

Problem Description:

  Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself.
  To make everything right, Petya is going to move at most one bracket from its original place in the sequence to any other position. Reversing the bracket (e.g. turning "(" into ")" or vice versa) isn't allowed.
We remind that bracket sequence s is called correct if:

  1. s is empty;
  2. s is equal to "(t)", where t is correct bracket sequence;
  3. s is equal to t1t2, i.e. concatenation of t1 and t2, where t1 and t2 are correct bracket sequences.

For example, "(()())", "()" are correct, while ")(" and "())" are not. Help Petya to fix his birthday present and understand whether he can move one bracket so that the sequence becomes correct.
Input
First of line of input contains a single number n (1≤n≤200000) — length of the sequence which Petya received for his birthday.
Second line of the input contains bracket sequence of length n, containing symbols "(" and ")".
Output
Print "Yes" if Petya can make his sequence correct moving at most one bracket. Otherwise print "No".

Input1

)(

Output1

Yes

Input2

(()

Output2

No

Input3

()

Output3

Yes

题意:给出字符串长度,和一段只含左右括号的字符,并定义该字符序列是好的条件为括号匹配或者只通过移一个括号,能使其完全匹配,如果满足上述条件,则输出Yes,否则输出No。

思路:用栈模拟括号匹配.最后判断栈中元素是否只有  )  (   这 两种括号即可.

AC代码:

#include<bits/stdc++.h>

using namespace std;

int main(){
int n;
cin>>n;
string str;cin>>str;
stack<char> s;
if(n%){
printf("No");return ;
}
for(int i=;i<n;i++){
if(s.empty()){
s.push(str[i]);
}else{
if(str[i]==')'){
char temp=s.top();
if(temp=='('){
s.pop();
}else{
s.push(str[i]);
}
}else{
s.push(str[i]);
}
}
}
if(s.empty()){
printf("Yes\n");return ;
}else{
if(s.size()!=){
printf("No");return ;
}else{
char t1=s.top();s.pop();
char t2=s.top();s.pop();
if(t1=='('&&t2==')'){
printf("Yes\n");
}else{
printf("No\n");
}
}
}
return ;
}

Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) C题的更多相关文章

  1. Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题

    A. Optimal Currency ExchangeAndrew was very excited to participate in Olympiad of Metropolises. Days ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  3. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  4. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  5. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  6. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  7. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  8. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  9. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

随机推荐

  1. sql根据时间戳按年月日分组统计

    sql根据时间戳按年月日分组统计,用于按日期分类: create_time为时间格式(字段名create_time 根据自己表字段修改,数据库中存为201610071202) SELECT DATE_ ...

  2. DMA存储器到外设代码讲解

    实验目的: bsp_dma_mtp.h #ifndef __BSP_DMA_MTP_H #define __BSP_DMA_MTP_H #include "stm32f10x.h" ...

  3. IDEA整合Jenkins界面化管理项目构建

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/WALK_MAN_wubiao/articl ...

  4. (六)mybatis之多对一关系(简单)

    一.需求分析 需求:   查询所有订单信息及订单下的订单明细信息 分析:      一条订单只能由一个消费者下单,但是一条订单有多条订单明细. 二.创建数据库表和实体对象 Customer.java ...

  5. (二)第一个mybatis项目

    1. 引包 本例中使用maven构造项目,所以只需配置依赖即可引相应的包. pom.xml <project xmlns="http://maven.apache.org/POM/4. ...

  6. android 和 js 交互

    1.html代码 <script type="text/javascript"> function javacalljs(){ document.getElementB ...

  7. C++ 容器一图以蔽之

    读完C++ primary 容器相关章节,有必要总结一下容器的要点,一图说明. 其中的问题,以下是我的一些想法,欢迎交流. 问题1. STL源码剖析 · vector 问题2. STL源码剖析 · R ...

  8. 周伯通的空明拳,米诺斯的星尘傀儡线,SAP Kyma的Serverless

    Jerry一直认为,金庸的<天龙八部>里的武学建模已经有点脱离传统武侠小说的范畴了,像已经走上玄幻道路的灵鹫宫"八荒六合唯我独尊功",以及杀伤力足够能被视为现代激光武器 ...

  9. 6.显示锁Lock 和 线程通信Condition

    显示锁 Lock 一.用于解决多线程 安全问题的方式: synchronized:   1.同步代码块      2.同步方法 jdk1.5 后:第三种:同步锁Lock  (注意:同步(synchro ...

  10. ASE19团队项目 beta阶段 model组 scrum6 记录

    本次会议于12月9日,19时30分在微软北京西二号楼sky garden召开,持续20分钟. 与会人员:Jiyan He, Lei Chai, Linfeng Qi, Xueqing Wu, Kun ...