CF1214C.Bad Sequence
普及-
通过率:0%
时间限制:1.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself.
To make everything right, Petya is going to move at most one bracket from its original place in the sequence to any other position. Reversing the bracket (e.g. turning "(" into ")" or vice versa) isn't allowed.
We remind that bracket sequence s is called correct if:
- s is empty;
- s is equal to "(t)", where t is correct bracket sequence;
- s is equal to t1t2, i.e. concatenation of t1 and t2, where t1 and t2 are correct bracket sequences.
For example, "(()())", "()" are correct, while ")(" and "())" are not. Help Petya to fix his birthday present and understand whether he can move one bracket so that the sequence becomes correct.
佩佳的朋友们送给他一个生日礼物——一个括号序列。佩佳对这个礼物感到非常失望,因为他梦想得到的是一个合法括号序列,但他并未向朋友们透露自己的愿望,而是决定自己来修复这份礼物。
为了使一切变得正确,佩佳最多可以将一个括号从它在原序列中的位置移动到任意其他位置(注意:不允许翻转括号,即不能将 "(" 变为 ")" 或反之)。
我们回顾一下:括号序列 s 被称为合法的,当且仅当满足以下条件之一:
- s 为空;
- s 形如 "(t)",其中 t 是合法括号序列;
- s 形如 t1t2,即两个合法括号序列 t1 和 t2 的拼接。
例如,"(())()"、"()" 是合法的,而 ")(" 和 "())" 则不是。请帮助佩佳修复他的生日礼物,并判断他是否能通过移动恰好一个括号(或不移动)使得该序列变为合法括号序列。
输入格式
First of line of input contains a single number n (1≤n≤200000) — length of the sequence which Petya received for his birthday.
Second line of the input contains bracket sequence of length n, containing symbols "(" and ")".
输入的第一行包含一个整数 n(1≤n≤200000)—— 表示佩佳生日收到的括号序列的长度。
输入的第二行包含一个长度为 n 的括号序列,仅由字符 "(" 和 ")" 组成。
输出格式
Print "Yes" if Petya can make his sequence correct moving at most one bracket. Otherwise print "No".
如果Petya最多移动一个括号就能使他的序列变为合法序列,则输出“Yes”;否则输出“No”。
输入输出样例
输入#1
2 )(
输出#1
Yes
输入#2
3 (()
输出#2
No
输入#3
2 ()
输出#3
Yes
输入#4
10 )))))(((((
输出#4
No
说明/提示
In the first example, Petya can move first bracket to the end, thus turning the sequence into "()", which is correct bracket sequence.
In the second example, there is no way to move at most one bracket so that the sequence becomes correct.
In the third example, the sequence is already correct and there's no need to move brackets.
在第一个例子中,Petya 可以将第一个左括号移动到末尾,从而将序列变为 "()",这是一个合法的括号序列。
在第二个例子中,不存在最多移动一个括号使得序列变为合法括号序列的方法。
在第三个例子中,该序列本身已是合法括号序列,无需移动任何括号。
输入解题思路,AI测评打分。不知道怎么写?