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

  1. )(

Output1

  1. Yes

Input2

  1. (()

Output2

  1. No

Input3

  1. ()

Output3

  1. Yes

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

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

AC代码:

  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6. int n;
  7. cin>>n;
  8. string str;cin>>str;
  9. stack<char> s;
  10. if(n%){
  11. printf("No");return ;
  12. }
  13. for(int i=;i<n;i++){
  14. if(s.empty()){
  15. s.push(str[i]);
  16. }else{
  17. if(str[i]==')'){
  18. char temp=s.top();
  19. if(temp=='('){
  20. s.pop();
  21. }else{
  22. s.push(str[i]);
  23. }
  24. }else{
  25. s.push(str[i]);
  26. }
  27. }
  28. }
  29. if(s.empty()){
  30. printf("Yes\n");return ;
  31. }else{
  32. if(s.size()!=){
  33. printf("No");return ;
  34. }else{
  35. char t1=s.top();s.pop();
  36. char t2=s.top();s.pop();
  37. if(t1=='('&&t2==')'){
  38. printf("Yes\n");
  39. }else{
  40. printf("No\n");
  41. }
  42. }
  43. }
  44. return ;
  45. }

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. gdb 常用命令总结(精优)

    格式说明: [xxx]:可选参数,即可以指定可以不指定,实际输入的内容是 xxx <xxx>:占位参数,即必须指定的参数,实际输入的内容是 xxx gdb 常用命令: gdb [file] ...

  2. MongoDB 聚合函数及排序

    聚合函数 最大值  $max db.mycol.aggregate([{$group : {_id : "$by_user", num_max : {$max: "$li ...

  3. WUSTOJ 1291: 2n皇后问题(Java)

    题目:

  4. Python之字符与编码笔记

    概述 类型 str 字符串 bytes 字节 bytearray 字节数组 字符串编码架构 字符集:赋值一个编码到某个字符,以便在内存中表示 编码 Ecoding:转换字符到原始字节形式 解码 Dec ...

  5. System函数的使用说明

    #inlcude<stdlib.h> int system(const char* command) 功能:在已经运行的程序中调用另一个外部程序 参数:外部可执行程序的名字 返回值:不同系 ...

  6. 浅析ARM协处理器CP15寄存器有关指令:MCR\MRC

    ref:http://blog.csdn.net/gameit/article/details/13169405 背景: 在uboot中,start.s中涉及到了 CP15 的有关操作.查阅有关资料, ...

  7. CentOS7 服务器连接超时自动断开问题解决

    背景 现在的客户们都开始使用云服务器了,还要通过堡垒机才给访问权限,这种方式访问方式以前都是银行的"专利",不过也间接说明其他行业的信息化也越来越普及了.今天主要是因为分配给我的这 ...

  8. springboot mvc自动配置(一)自动配置DispatcherServlet和DispatcherServletRegistry

    所有文章 https://www.cnblogs.com/lay2017/p/11775787.html 正文 springboot的自动配置基于SPI机制,实现自动配置的核心要点就是添加一个自动配置 ...

  9. python 操作Hbase 详解

    博文参考:https://www.cnblogs.com/tashanzhishi/p/10917956.html 如果你们学习过Python,可以用Python来对Hbase进行操作. happyb ...

  10. inotify和rsync实现数据实时同步

    数据的实时同步 实现实时同步 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 发现目录中数据产生变化,就利用rsync服务推送到备份服务器上 实现实时同步的方法 ino ...