A12772 | Bad Sequence
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $t_1 t_2$ , i.e. concatenation of $t_1$ and $t_2$ , where $t_1$ and $t_2$ 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.
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 $t_1 t_2$ , i.e. concatenation of $t_1$ and $t_2$ , where $t_1$ and $t_2$ 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.
输入格式
First of line of input contains a single number $n$ ( $1 \leq n \leq 200\,000$ ) — length of the sequence which Petya received for his birthday.
Second line of the input contains bracket sequence of length $n$ , containing symbols "(" and ")".
Second line of the input contains bracket sequence of length $n$ , containing symbols "(" and ")".
输出格式
Print "Yes" if Petya can make his sequence correct moving at most one bracket. Otherwise print "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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted