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 < ...
随机推荐
- ubuntu系统里常用的几个命令
### ubuntu系统里常用的几个命令 卸载软件: sudo apt-get --purge remove easy-rsa //最后是包名, --purge是可选参数,加上的话移除配置文件 删除文 ...
- PAT(B) 1012 数字分类(Java)
题目链接:1012 数字分类 代码 /** * Score 20 * Run Time 142ms * @author wowpH * @version 1.1 */ import java.util ...
- Oracle查询部门工资最高员工的两种方法 1、MAX()函数 2、RANK()函数
本文以SCOTT用户下初始的EMP表为参考.代码可直接使用. 查询EMP表结构的语句如下,[代码1]: DESC EMP; EMP表结构如下:[结果1]: SQL> DESC EMP ...
- go 通过赋值给 _ 来忽略序号和值
go 语言中 只要想忽略的值都需要用 下划线 _ 来代替 package main import "fmt" func main() { pow := make([]int ...
- Windows下Notepad++连接VMWare中的linux,然后无法安装NppFTP
一.关于Notepad++版本 我的版本是最新版本:Notepad++ v7.7 32bit 版本最好选择32bit的,看别处的说法是官网上有这样的说明: Note that the most of ...
- Java容器汇总【红黑树需要再次学习】
1,概述 2,Collection 2.1,Set[接触比较少] 2.1.1 TreeSet 底层由TreeMap实现 基于红黑树实现,支持有序性操作,例如根据一个范围查找元素的操作.但是查找效率不如 ...
- centOS 7单机安装 kong
kong 网关 单机部署 环境:centOS 7:依赖:jdk1.8 安装内容:postgresql数据库, kong 网关,nodeJs和npm,kong Dashboard (可视化管理界面) 版 ...
- springboot mvc自动配置(三)初始化mvc的组件
所有文章 https://www.cnblogs.com/lay2017/p/11775787.html 正文 在springboot mvc自动配置的时候,获得了DispatcherServlet和 ...
- swith-case 日历
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Go 结构体的使用
结构体是用户定义的类型,表示若干个字段(Field)的集合.有时应该把数据整合在一起,而不是让这些数据没有联系.这种情况下可以使用结构体. 例如,一个职员有 firstName.lastName 和 ...