#abc307d. D - Mismatched Parentheses

D - Mismatched Parentheses

Score : 400400 points

问题描述

你将得到一个长度为 NN 的字符串 SS,由小写英文字母和字符 () 组成。
在执行以下操作尽可能多次后,打印字符串 SS

  • 选择并删除一个以 ( 开始、以 ) 结束且除首尾字符外不包含其他 () 的连续子串。

可以证明,无论该操作如何执行,执行尽可能多次后得到的字符串 SS 是唯一确定的。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a string SS of length NN consisting of lowercase English letters and the characters ( and ).
Print the string SS after performing the following operation as many times as possible.

  • Choose and delete a contiguous substring of SS that starts with (, ends with ), and does not contain ( or ) other than the first and last characters.

It can be proved that the string SS after performing the operation as many times as possible is uniquely determined without depending on how it is performed.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • NN is an integer.
  • SS is a string of length NN consisting of lowercase English letters and the characters ( and ).

Input

The input is given from Standard Input in the following format:

NN

SS

Output

Print the answer.

Sample Input 1

8
a(b(d))c

Sample Output 1

ac

Here is one possible procedure, after which SS will be ac.

  • Delete the substring (d) formed by the fourth to sixth characters of SS, making it a(b)c.
  • Delete the substring (b) formed by the second to fourth characters of SS, making it ac.
  • The operation can no longer be performed.

Sample Input 2

5
a(b)(

Sample Output 2

a(

Sample Input 3

2
()

Sample Output 3

The string SS after the procedure may be empty.

Sample Input 4

6
)))(((

Sample Output 4

)))(((

update @ 2024/3/10 08:41:37