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 ...
随机推荐
- Http post请求案例
public RmiRespBase sendHttpRes(String jsonParamStr, String url, String apiName,String systemId,Strin ...
- DOM4J -(XML解析包)
DOM4J - 简介 是dom4j.org出品的一个开源XML解析包.Dom4j是一个易用的.开源的库,用于XML,XPath和XSLT.它应用于Java平台,采用了Java集合框架并完全支持DOM, ...
- IDEA Maven打包时去掉test
- 没有ORM库的时候,通过PDO连接MySQL的方法
$pdo = new PDO("mysql:host=localhost;dbname=eq","root","root"); $pdo-& ...
- step()动画
<style type="text/css"> .hi { width: 50px; height: 72px; background-image: url(" ...
- https方式nginx 代理tomcat访问不带www的域名301重定向跳转到www的域名帮助seo集中权重
比如我要把 http://gucanhui.com http://www.gucanhui.com 跳转到https://www.gucanhui.com 用F12的network可以看到状态码301 ...
- Python学习笔记(六)Python组合数据类型
在之前我们学会了数字类型,包括整数类型.浮点类型和复数类型,这些类型仅能表示一个数据,这种表示单一数据的类型称为基本数据类型.然而,实际计算中却存在大量同时处理多个数据的情况,这种需要将多个数据有效组 ...
- SpringBooot-基础<2>-POM.xml配置
SpringBooot-基础<2>-POM.xml配置 项目创建完成后,需要配置pom.xml文件. pom.xml里面的配置,按需进行添加,这里提供一份参考,后面做笔记会都用到. < ...
- centos 安装nginx + 多个tomcat负载均衡
今天在centos上安装了两个tomcat和nginx,进行配置.今天记录的只是最基本的实现测试.(不包含使用redis进行session共享) Nginx 是一款轻量级的Web 服务器/反向代理服务 ...
- 八.DBN深度置信网络
BP神经网络是1968年由Rumelhart和Mcclelland为首的科学家提出的概念,是一种按照误差反向传播算法进行训练的多层前馈神经网络,是目前应用比较广泛的一种神经网络结构.BP网络神经网络由 ...