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 ...
随机推荐
- 【DM642学习笔记六】TI参考文档--DM642 Video Port Mini Driver
这个文档介绍了在DM642EVM板上视频采集和显示微驱动的使用和设计.用EDMA进行存储器和视频端口的数据传输.为了增强代码的复用性和简化设计过程,驱动分为通用视频端口层和特定编解码芯片微驱动层两个 ...
- 关于mybatis-config.xml文件的基础解释
今天是我第一天落户博客园,想一想从mybatis框架开始写起吧.mybatis框架与Hibernate框架相比来说,专注于SQL语句,对SQL语句的要求比较高吧. 我觉得,对于mybatis框架来说, ...
- Python爬虫笔记【一】模拟用户访问之验证码清理(4)
清理图片,对图片进行二值化,去边框,去干扰线,去点 from PIL import Image from pytesseract import * from fnmatch import fnmatc ...
- 卸载VS2015之后,安装VS2017出错
安装出现问题. 可通过以下方式排查包故障问题: 1. 使用以下搜索 URL 来搜索针对每个包故障的解决方案 2. 针对受与影响的工作负荷或组件修改选项,然后重新尝试安装 3. 从计算机上删除产品,然后 ...
- python生成VOC2007的类库
VOCAnnotation.py: # -*-coding:utf-8-*- from lxml import etree import os class VOCAnnotation(object): ...
- Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
摘要: 本文对Hystrix.Resilience4j.Sentinel进行对比,并探讨如何使用一行代码这种极简的方式,将Hystrix迁移到Sentinel. Hystrix 自从前段时间 宣布停止 ...
- [原创]iFPGA-Cable FT2232H Xilinx / Altera / Lattice 三合一JTAG & UART调试器-详细使用说明
iFPGA-Cable调试器使用说明 全文分为6部分: 第0部分:实物.连线及其驱动安装说明 第1部分:Xilinx JTAG 第2部分:UART 第3部分:Altera JTAG 第4部分:Latt ...
- 字符串常用方法(转载--https://www.cnblogs.com/ABook/p/5527341.html)
一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...
- 微信小程序 this.setData() 详解
1.定义 setData()函数用于将逻辑层数据发送到视图层,同时对应的改变this.data的值. 2.setData()参数格式 接受一个对象,以键(key)值(value)的方式改变值. 其中, ...
- java.lang.NoSuchMethodError: org.apache.commons.io.FileUtils.copyInputStreamToFile(Ljava/io/InputStream;Ljava/io/File;)V
昨天用的好好的,今天就不行了 也懒得搞 具体原因就是引用的问题 找了个能用的pom 直接贴过去完事儿