Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) C题
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:
- s is empty;
- s is equal to "(t)", where t is correct bracket sequence;
- 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题的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- 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 ...
- 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 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- 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 < ...
随机推荐
- C++之父给 C 程序员的建议
1. 在 C++中几乎不需要用宏, 用 const 或 enum 定义显式的常量, 用 inline 避免函数调用的额外开销,用模板去刻画一族函数或类型,用 namespace 去避免命名冲突. 2. ...
- Linux centos 7下搭建mosquitto
Centos7安装 1.网卡名改为enth0 A: vim /etc/sysconfig/grub B: 第三行添加"net.ifnames=0 biosdevname=0" ...
- go if 判断 完成随机分数的评级
1 go中 所有的大括号要跟在 当前语句的后面不能换行 例如: if a>0 { func getUser(){ for { 2关于随机分数的生成 种子的设置放到循环中会是重复的数字,这是可以 ...
- 2019牛客多校三 A. Graph Games (图分块)
大意: 给定无向图, 定义$S(x)$为$x$的邻接点集合. 每次操作翻转$[L,R]$范围的边, 询问$S(x)$与$S(y)$是否相等. 快速判断集合相等可以用$CF 799F$的技巧. 采用$h ...
- (六)maven之常用插件
一.maven的插件 maven官方插件:http://maven.apache.org/plugins/index.html 二.常用插件之:javadoc 作用:生成代码文档 2.1 编写代码,并 ...
- linux之find的使用
基本语法 find [查找目录] [选项] [查找规则] [查找完后的操作] 即:find pathname -option -condition [-print -exec -ok …] 选项参数 ...
- js对象 c#对象转换
前台页面 js 创建对象 let t = {}; 数组对象 let c = []; c.push({}) ;// 添加对象 以string格式 传递 JSON JSON.stringify(c); c ...
- ES6语法 学习
ECMAScript 6,也被称为ECMAScript 2015是ECMAScript标准的最新版本.6是语言的一个重要更新,并第一次更新语言由于ES5 2009标准.现在主要JavaScript引擎 ...
- CSS过渡效果transition和动画
一.过渡效果 可以在不适用Flash和js 的情况下实现过渡效果 属性 描述 transition 简写属性,用于在一个属性中设置四个过渡属性 transition-property 规定应用过渡的c ...
- stm32 红外
相关文章:http://blog.csdn.net/zhangxuechao_/article/details/75039906 举例 u8 ir_tick() //记录高电平时间 { u8 i = ...