C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)
冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C
1 second
256 megabytes
standard input
standard output
Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.
In his favorite math class, the teacher taught him the following interesting definitions.
A parenthesis sequence is a string, containing only characters "(" and ")".
A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition.
We define that |s||s| as the length of string ss. A strict prefix s[1…l]s[1…l] (1≤l<|s|)(1≤l<|s|) of a string s=s1s2…s|s|s=s1s2…s|s| is string s1s2…sls1s2…sl. Note that the empty string and the whole string are not strict prefixes of any string by the definition.
Having learned these definitions, he comes up with a new problem. He writes down a string ss containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in ss independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence.
After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable.
The first line contains a single integer |s||s| (1≤|s|≤3⋅1051≤|s|≤3⋅105), the length of the string.
The second line contains a string ss, containing only "(", ")" and "?".
A single line contains a string representing the answer.
If there are many solutions, any of them is acceptable.
If there is no answer, print a single line containing ":(" (without the quotes).
6
(?????
(()())
10
(???(???(?
:(
It can be proved that there is no solution for the second sample, so print ":(".
解题思路:
括号匹配问题,但要求 严格前缀不满足括号匹配,但是整个字符满足括号匹配。
解题思路:
括号匹配easy,给一组数据:
8
((??))))
答案:(((())))
如何处理 ' ? ' 这里有一个贪心, 就是如果满足括号匹配,那么左括号和右括号肯定各占一半。
先扫一遍,统计已出现的左括号和右括号,
然后再扫一遍 如果遇到 ' ? ' 先满足达到 N/2 个 左括号(左括号肯定越前越好), 然后再满足 N/2 个右括号。
最后扫一遍判断修改后的字符串是否满足括号匹配,如果中途出现栈空的情况说明有严格前缀也符合括号匹配,则不行;最后栈不为空也不行,否者输出答案。
AC code:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
#define inc(i, j, k) for(int i = j; i <= k; i++)
#define rep(i, j, k) for(int i = j; i < k; i++)
#define mem(i, j) memset(i, j, sizeof(i))
#define gcd(i, j) __gcd(i, j)
using namespace std;
string ss;
stack<char>mmp;
int N, a, b;
int main()
{
scanf("%d", &N);
cin >> ss;
if(N%){ puts(":("); return ;}
for(int i = ; i < N; i++)
{
if(ss[i] == '(') a++;
else if(ss[i] == ')') b++;
}
bool flag = true;
int n1 = N/-a;
int n2 = N/-b;
int k = ;
for(int i = ; i < n1;k++){
if(ss[k] == '?'){ ss[k] = '('; i++;}
}
for(int j = ; j < n2; k++){
if(ss[k] == '?'){ ss[k] = ')';j++;}
}
// cout << ss << endl;
for(int i = ; i < N; i++){
if(mmp.size() == ){
mmp.push(ss[i]);
continue;
}
if(ss[i] == '('){
mmp.push(ss[i]);
}
else{
if(mmp.top() == '(') mmp.pop();
else mmp.push(ss[i]);
if(mmp.size() == && i != N-){ flag = false;break; }
}
}
if(mmp.size() == && flag) cout << ss << endl;
else puts(":("); return ;
}
C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)的更多相关文章
- 【Codeforces】Codeforces Round #551 (Div. 2)
Codeforces Round #551 (Div. 2) 算是放弃颓废决定好好打比赛好好刷题的开始吧 A. Serval and Bus 处理每个巴士最早到站且大于t的时间 #include &l ...
- Codeforces Round #551 (Div. 2) A-E
A. Serval and Bus 算出每辆车会在什么时候上车, 取min即可 #include<cstdio> #include<algorithm> #include< ...
- Codeforces Round #551 (Div. 2) A~E题解
突然发现上一场没有写,那就补补吧 本来这场应该5题的,结果一念之差E fail了 A. Serval and Bus 基本数学不解释,假如你没有+1 -1真的不好意思见人了 #include<c ...
- Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)
题目:http://codeforces.com/contest/1153/problem/D 题意:给你一棵树,每个节点有一个操作,0代表取子节点中最小的那个值,1代表取子节点中最大的值,叶子节点的 ...
- Codeforces Round #551 (Div. 2)A. Serval and Bus
A. Serval and Bus time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #551 (Div. 2)B. Serval and Toy Bricks
B. Serval and Toy Bricks time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)
题目链接 题意:给你一个有根树,假设有k个叶子节点,你可以给每个叶子节点编个号,要求编号不重复且在1-k以内.然后根据节点的max,minmax,minmax,min信息更新节点的值,要求根节点的值最 ...
- Codeforces Round #551 (Div. 2) E. Serval and Snake (交互题)
人生第一次交互题ac! 其实比较水 容易发现如果查询的矩阵里面包含一个端点,得到的值是奇数:否则是偶数. 所以只要花2*n次查询每一行和每一列,找出其中查询答案为奇数的行和列,就表示这一行有一个端点. ...
- Codeforces Round #551 (Div. 2) F. Serval and Bonus Problem (DP/FFT)
yyb大佬的博客 这线段期望好神啊... 还有O(nlogn)FFTO(nlogn)FFTO(nlogn)FFT的做法 Freopen大佬的博客 本蒟蒻只会O(n2)O(n^2)O(n2) CODE ...
随机推荐
- 请比较throw 合throws的区别
throw语句用在方法体内,表示抛出异常.throws语句用在方法声明的后面,表示再抛出异常,由该方法的调用者来处理.throws主要声明这个方法会抛出这种类型的异常,使它的调用者知道要捕获这个异常. ...
- Eclipse发布的Dynamical web项目在Tomacat文件夹下显示
Eclipse设置了Tomacat后,项目信息会在你的workspace上,在Tomacat文件夹上是没有的.但是通过设置是可以在Tomacat文件夹上存在的. 配置好服务器后,先关闭服务器,然后在E ...
- java设计模式-----15、适配器模式
概念: Adapter模式也叫适配器模式,是构造型模式之一,通过Adapter模式可以改变已有类(或外部类)的接口形式. 举个例子:我们使用电脑,家里的电源是220V的,而我们的电脑是18V的,这时如 ...
- linux系统下开启一个简单的web服务
linux 下开启一个简单的web服务: 首先需要linux下安装nodejs 然后创建一个test.js: vi test.js var http =require("http&quo ...
- JS实现的数组全排列输出算法
本文实例讲述了JS实现的数组全排列输出算法.分享给大家供大家参考.具体分析如下: 这段js代码对数组进行全排列输出,改进了一些老的代码 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来, ...
- opencv3.2.0实现连续图片合成avi视频
##名称:利用videowriter实现多张连续图片合成avi视频 ##平台:QT5.7.1+OpenCV3.2.0 ##日期:2017年12月10日 /**************新建QT控制台程序 ...
- Dynamics 365 Customer Engagement 中对API的调整内容分享
当前版本中弃用了以下客户端 API 以重新组织 Xrm 客户端 API 对象模型,从而更好地满足以下需求:使用同一客户端脚本而不必基于上下文或基于运行这些脚本的客户端(Web 客户端或新的统一接口)来 ...
- 葡萄城报表介绍:B/S 报表软件
B/S 报表软件定义 B/S(Browser/Server,浏览器/服务器模式)也称 B/S 结构,是 WEB 兴起后的一种网络结构模式.B/S 模式是由最开始的 C/S(Client/Server, ...
- Pig store用法举例
store:将数据存储到HDFS等文件系统里 将数据保存到/data目录 store data into '/data'; 以逗号为分隔符 store data into '/data' usin ...
- Re:LieF ~親愛なるあなたへ~ 后感
遇到烦恼就能有个安逸的地方逃避.这个想法真好.遗憾现实并不能如此.若是觉得这款纯爱作有些许的感人之处,那定时因为受众玩家正在通过玩游戏来逃避现实.“虚拟世界的感情是真实的.” 这件事在旁人看来或许是笑 ...