题目大意:一个字符串只含有? ( ),?可以变成 ) 或者 ( ,将字符串中所有的?变成) 或者 ( 使得字符串合法。

合法就是让括号配对,并且不可以提前结束比如:()()这样是不合法的。

题解:既然不能提前结束,那第一个字符必须和最后一个匹配,所以我们只要关注从2~n-1就可以了。

我们可以正着扫一遍,在任何一个位置")"的数目必须小于等于"("+"?"。

然后再倒着扫一遍,同样地,在任何位置的"("的数目必须小于等于")"+"?"

在新的串中,"("=")"=(n-2)/2,然后让前边的问好补""(",剩下的问好补")"。

code:

#include<bits/stdc++.h>
using namespace std;
const int N=3E5+;
char s[N];
void solve(){
int n;
cin>>n;
scanf("%s",s+);
if((n&)||s[]==')'||s[n]=='('){
puts(":(");
return ;
}
int t1=,t2=,t3=;
for(int i=;i<=n-;i++){
if(s[i]=='(') t1++;
else if(s[i]==')') t2++;
else t3++;
if(t2>t1+t3){
puts(":(");
return ;
}
}
t1=,t2=,t3=;
for(int i=n-;i>=;i--){
if(s[i]=='(') t1++;
else if(s[i]==')') t2++;
else t3++;
if(t1>t2+t3){
puts(":(");
return ;
}
}
int c=(n-)/;
if(t1>c||t2>c) {
puts(":(");
return ;
}
int tmp1=;
cout<<"(";
for(int i=;i<=n-;i++){
if(s[i]==')'||s[i]=='(') cout<<s[i];
else {
if(tmp1<c-t1) {
cout<<"(";
tmp1++;
}
else cout<<")";
}
}
cout<<")";
puts("");
}
int main(){
solve();
return ;
}

Serval and Parenthesis Sequence CodeForces - 1153C的更多相关文章

  1. C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)

    冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...

  2. CF1153C Serval and Parenthesis Sequence

    题目地址:CF1153C Serval and Parenthesis Sequence 思路:贪心 如果有解,那么 \(s_0 = (\) && \(s_{n-1} = )\) &a ...

  3. Serval and Parenthesis Sequence【思维】

    Serval and Parenthesis Sequence 题目链接(点击) Serval soon said goodbye to Japari kindergarten, and began ...

  4. cf-Round551-Div2-C. Serval and Parenthesis Sequence(贪心)

    题目链接:http://codeforces.com/contest/1153/problem/C 题意:给定由'(',')','?'组成的字符串,问是否能将其中的?全部换成'(‘,’)'使得字符串的 ...

  5. cf——C. Serval and Parenthesis Sequence

    括号正确匹配问题,应该不难 #include <iostream> #include <cstring> #include <string> #include &l ...

  6. Increasing Sequence CodeForces - 11A

    Increasing Sequence CodeForces - 11A 很简单的贪心.由于不能减少元素,只能增加,过程只能是从左到右一个个看过去,看到一个小于等于左边的数的数就把它加到比左边大,并记 ...

  7. Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)

    Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...

  8. AC日记——The Child and Sequence codeforces 250D

    D - The Child and Sequence 思路: 因为有区间取模操作所以没法用标记下传: 我们发现,当一个数小于要取模的值时就可以放弃: 凭借这个来减少更新线段树的次数: 来,上代码: # ...

  9. C. Vasily the Bear and Sequence Codeforces 336C(枚举,思维)

    C. Vasily the Bear and Sequence time limit per test 1 second memory limit per test 256 megabytes inp ...

随机推荐

  1. tcp\udp 操作系统发展史

    目录 为什么会出现粘包现象 socket发送大文件示例 UDP协议 基于UDP实现简易版本的qq 总结: SocketServer模块介绍(让tcp也能支持并发) 为什么会出现粘包现象 TCP 三次握 ...

  2. 洛谷 P3870 [TJOI2009]开关 题解

    原题链接 前置知识: 线段树的单点.区间的修改与查询. 一看,我们需要维护两个操作: 区间取反: 区间求和. (因为区间 \(1\) 的个数,就是区间的和) 典型的 线段树 . 如果你只会线段树的 区 ...

  3. Reverse Subarray To Maximize Array Value

    2020-02-03 20:43:46 问题描述: 问题求解: public boolean canTransform(String start, String end) { int n = star ...

  4. 2、Spark Core职责之初始化(1)——SparkContext

    SparkContext(Spark上下文) /** * Main entry point for Spark functionality. A SparkContext represents the ...

  5. MATLAB GUI 预约程序

    因为一起奇怪的原因,要做一个预约程序.初衷是能够完成注册.登陆.预约.查看个人信息等. 原本想用Java写的,又由于一些特殊原因耽搁了,导致最后只有一个晚上的时间,匆匆忙忙到最后就用MATLAB GU ...

  6. 干货|漫画算法:LRU从实现到应用层层剖析(第一讲)

    今天为大家分享很出名的LRU算法,第一讲共包括4节. LRU概述 LRU使用 LRU实现 Redis近LRU概述 第一部分:LRU概述 LRU是Least Recently Used的缩写,译为最近最 ...

  7. iOS 状态栏显示/隐藏

    iOS 10为止,将在plist文件中将 View controller-based status bar appearance 设置为NO,就可以用UIApplication设置状态栏. 注意,要在 ...

  8. Make编译Ardupilot源码的两种方法

    编译环境准备 ​ Ardupilot源码下载和PX4 toolchain工具链下载 ​ (见https://www.cnblogs.com/BlogsOfLei/p/7707485.html) ​ 注 ...

  9. B 雷诺与奴隶主

    时间限制 : 5000 MS   空间限制 : 524288 KB 问题描述 自从周文武老师在班上吹嘘了"录试"这款游戏之后,PHD同学沉迷于其中无法自拔. "录试&qu ...

  10. 基于 HTML5 WebGL 的楼宇智能化集成系统(二)

    前言       一套完整的可视化操作交互上,必不可少 2D/3D 的融合,在上期我们介绍了有关 3D 场景的环视漫游.巡视漫游以及动画效果,还包括了冷站场景.热站场景以及智慧末端的实现原理,本期主要 ...