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. Python 基础教程 | 菜鸟教程

    https://www.runoob.com/python/python-install.html

  2. destoon 增删改查

    switch($action) { case 'add': //添加页面 if($submit) { // 不允许重名,直接添加时 $old = $db->get_one("SELEC ...

  3. SpringCloud Eureka 配置

    修改 Eureka server 注册中心上面显示的服务名称 参数配置 默认值     说明 服务注册中心配置   Bean类:org.springframework.cloud.netflix.eu ...

  4. css 背景 background

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! background我们一般用到的的属性有: background-attachment:背景(图片)是否 ...

  5. (四)自定义多个Realm以及Authenticator与AuthenticationStrategy

    多Realm配置 #声明一个realm myRealm1=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1 myRealm2=com.githu ...

  6. cnn健康增胖和调理好身体

    吃不胖,其实大部分情况是消化系统不好,大部分食物都没有被身体吸收就被排掉了. 1,改善肠胃消化功能: 每天早上一杯全脂鲜牛奶(或者羊奶), 每天晚上一杯酸奶 ps:白天和鲜牛奶可以激发肠胃的消化能力. ...

  7. SQL Server注入

    1.利用错误消息提取信息 1.1 枚举当前表与列 --' 抛出错误:选择列表中的列 'users.id' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中. 发现表名为 'users',存 ...

  8. SVN 问题解决之 Working copy path does not exist in repository

    同事的SVN更新时提示某个特定文件提示 Working copy path does not exist in repository svn更新会被这个错误打断,导致无法完全更新 删掉文件再更新仍然有 ...

  9. JS有关引用对象的拷贝问题

    JS中有关引用对象的拷贝问题 问题描述:在开发过程中,拷贝一个对象数组给另一个数组的时候,改变新数组中对象的属性值,原数组中的对象属性值也跟着改变了. 例如新定义一个数组arr1,里面有两个对象,然后 ...

  10. 如何在SAP Kyma的控制台里扩展新的UI

    方法是创建一个新的resource,类型为ClusterMicroFrontend. 使用命令行kubectl get ClusterMicroFrontend查看这些UI扩展: 最后自定义的UI出现 ...