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 < ...
随机推荐
- php去掉字符串中的最后一个字符和汉字
###php去掉字符串中的最后一个字符和汉字 1.php去掉字符串中的最后一个字符: //方法一: $newstr = substr($str,0,strlen($str)-1); //方法二: $n ...
- java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast
解决方案 在配置文件中配置proxy-target-class="true" <aop:aspectj-autoproxy proxy-target-class=" ...
- Session和Cookie的原理
1.session和cookie的存储 session一般保存在服务端文件中,php.ini中有个配置项--session.save_path='';这个里面填写的路径,将会使session文件保存在 ...
- C# Enum操作
枚举定义 /// <summary> /// 节点类型 /// </summary> public enum NodeTypeEnum { 企业 = , 人员 = , 人员地址 ...
- pymongo基础使用方法
本文通过文章同步功能推送至博客园,排版可能会有所错误,敬请见谅! 1.客户端初始化 初始化MongoDB客户端 client = pymongo.MongoClient('localhost',270 ...
- (九)mybatis之延迟加载
一.为什么要使用延迟加载? 使用延迟加载的意义 在进行数据查询时,为了提高数据库查询性能,尽量使用单表查询,因为单表查询比多表关联查询速度快. 如果查询单表就可以满足需求,一开始先查询单表,当需要关联 ...
- (七)Redis之Keys的通用操作
package myRedis01; import java.util.HashMap; import java.util.List; import java.util.Map; import jav ...
- PHP数字转大写
最近在研究算法,发现了一个数字转大写的算法挺有意思,分享给大家看看: function get_amount($num){ $c1 = "零壹贰叁肆伍陆柒捌玖"; ...
- .NET Core 使用ModelBinder去掉所有参数的空格
一.前言 通过各种姿势搜索都没搜到这方面的,唯一找到一个比较符合的,但是只适合简单类型,而且代码还没贴全,心累.. 然后查看官网和源码之后,发现继承并实现 IModelBinder和IModelBin ...
- sql 添加变量
在sql语句中添加变量. declare @local_variable data_type 声明时需要指定变量的类型, 可以使用set和select对变量进行赋值, 在sql语句中就可以使用@loc ...