AtCoder Beginner Contest 064 D - Insertion
AtCoder Beginner Contest 064 D - Insertion
Problem Statement
You are given a string S of length N consisting of ( and ). Your task is to insert some number of ( and ) into S to obtain a correct bracket sequence.
Here, a correct bracket sequence is defined as follows:
()is a correct bracket sequence.- If X is a correct bracket sequence, the concatenation of
(, X and)in this order is also a correct bracket sequence. - If X and Y are correct bracket sequences, the concatenation of X and Y in this order is also a correct bracket sequence.
- Every correct bracket sequence can be derived from the rules above.
Find the shortest correct bracket sequence that can be obtained. If there is more than one such sequence, find the lexicographically smallest one.
Constraints
- The length of S is N.
- 1≤N≤100
- S consists of
(and).
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the lexicographically smallest string among the shortest correct bracket sequences that can be obtained by inserting some number of ( and ) into S.
Sample Input 1
3
())
Sample Output 1
(())
Sample Input 2
6
)))())
Sample Output 2
(((()))())
Sample Input 3
8
))))((((
Sample Output 3
(((())))(((())))
题意就是给定长度的字符串加括号,使得括号相匹配。
不会。栈模拟半天也不知道怎么弄。看了别人的代码惊为天人!
一言以蔽之就是,先找出已经配对好的不管;剩下有多少个'('字符串后面就补多少个')'、有多少个')'字符串前面就补多少个'('。
直接从代码中领悟吧:
#include<iostream>
#include<string>
using namespace std; int main()
{
int n;
string s;
cin >> n;
cin >> s;
int totl = , totr = ;
for (int i = ; i < s.length(); i++) {
if (s[i] == '(') totl++;
else
{
if (totl > ) totl--;
else totr++;
}
}
for (int i = ; i < totl; i++) s = s + ')';
for (int i = ; i < totr; i++) s = '(' + s;
cout << s << endl;
return ;
}
AtCoder Beginner Contest 064 D - Insertion的更多相关文章
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
- AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】
AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...
- AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle【暴力】
AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle 我要崩溃,当时还以为是需要什么离散化的,原来是暴力,特么五层循环....我自己写怎么都 ...
- AtCoder Beginner Contest 075 C bridge【图论求桥】
AtCoder Beginner Contest 075 C bridge 桥就是指图中这样的边,删除它以后整个图不连通.本题就是求桥个数的裸题. dfn[u]指在dfs中搜索到u节点的次序值,low ...
随机推荐
- 轻松搞定 JS 的this、call和apply
年前最后一篇文章,提前祝大家春节快乐.对了,还有369就要2018年了,提前祝大家2018年春节快乐. .. .vr qBMBBBMBMY 8BBBBBOBMBMv iMBMM5vOY:BMBBv . ...
- nodejs+express 初学(一)
以下都是windows环境 1.下载nodejs http://www.nodejs.org/download/ 然后安装 2.确认已经安装完成 . node -v 3.安装express 注意: 是 ...
- 洛谷P5319 奥术神杖
题意:给你若干个串和一个填了一部分的串.补完这个串使得 (每个串的匹配次数 * 权值) ^ (1 / 所有串匹配次数) 最大. 解:把这个东西随便取一个对数,就变成了分数规划. 二分.然后在AC自动机 ...
- LuceneNet 搜索一
1.引用读取PDF文件组件 FontBox-0.1.0-dev.dll IKVM.GNU.Classpath.dll IKVM.Runtime.dll PDFBox-0.7.3.dll 2.添加off ...
- Django项目:CRM(客户关系管理系统)--22--14PerfectCRM实现King_admin分页的省略显示
{#table_data_list.html#} {## ————————08PerfectCRM实现King_admin显示注册表的字段表头————————#} {% extends 'king_m ...
- jeecms内容管理系统使用了哪些技术
jeeecms作为国内Java开源内容管理系统的龙头老大(感谢jeecms研发小组的辛苦劳作),技术架构也是比较成熟的,当然涉及到的技术也就相对多一些 看看jeecms内容管理系统使用了哪些技术 we ...
- Java的split()方法
在java中,split方法很常用,但是有些时候我们发现并不管用,不管用的情况:split("."),split("^"), 管用的情况:split(" ...
- 2019阿里云开年Hi购季新用户分会场全攻略!
2019阿里云云上Hi购季活动已经于2月25日正式开启,从已开放的活动页面来看,活动分为三个阶段: 2月25日-3月04日的活动报名阶段.3月04日-3月16日的新购满返+5折抢购阶段.3月16日-3 ...
- BZOJ1455罗马游戏
左偏树裸题. 题面描述让人意识到了平面几何的重要性. //Achen #include<algorithm> #include<iostream> #include<cs ...
- CentOS7安装步骤
CentOS7安装详解 本文基于vmware workstations进行CentOS7安装过程展示,关于vmware workstations安装配置本人这里不再介绍,基本过程相当于window ...