#abc350f. F - Transpose

F - Transpose

Score: 550550 points

问题陈述

给定一个字符串 S=S1S2S3SSS = S_1 S_2 S_3 \dots S_{|S|},由大写和小写英文字母以及 () 组成。 字符串 SS 中的括号是正确匹配的。

重复以下操作,直到无法再进行更多操作:

  • 首先,选择一对整数 (l,r)(l, r),满足以下所有条件:
    • l<rl < r
    • Sl=S_l = (
    • Sr=S_r = )
    • 字符 Sl+1,Sl+2,,Sr1S_{l+1}, S_{l+2}, \dots, S_{r-1} 都是大写或小写英文字母。
  • T=Sr1Sr2Sl+1T = \overline{S_{r-1} S_{r-2} \dots S_{l+1}}
    • 这里,x\overline{x} 表示通过切换字符串 xx 中每个字符的大小写(大写转小写,反之亦然)得到的字符串。
  • 然后,删除 SS 中的第 ll 个到第 rr 个字符,并将 TT 插入到删除发生的位置。

参考示例输入和输出以获得澄清。

可以证明,使用上述操作可以从字符串中移除所有的 (),并且最终字符串与操作的执行方式和顺序无关。 确定最终字符串。

SS 中的括号是正确匹配的意味着什么?首先,正确的括号序列定义如下:

  • 一个正确的括号序列是一个满足以下条件之一的字符串:

  • 它是一个空字符串。

  • 存在一个正确的括号序列 AA,该字符串由按顺序连接 (, AA, ) 形成。

  • 存在非空的正确括号序列 AABB,该字符串由按顺序连接 AABB 形成。

如果从 SS 中提取的 ()(不改变顺序)形成一个正确的括号序列,则称 SS 中的括号是正确匹配的。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a string S=S1S2S3SSS = S_1 S_2 S_3 \dots S_{|S|} consisting of uppercase and lowercase English letters, (, and ).
The parentheses in the string SS are properly matched.

Repeat the following operation until no more operations can be performed:

  • First, select one pair of integers (l,r)(l, r) that satisfies all of the following conditions:
    • l<rl < r
    • Sl=S_l = (
    • Sr=S_r = )
    • Each of the characters Sl+1,Sl+2,,Sr1S_{l+1}, S_{l+2}, \dots, S_{r-1} is an uppercase or lowercase English letter.
  • Let T=Sr1Sr2Sl+1T = \overline{S_{r-1} S_{r-2} \dots S_{l+1}}.
    • Here, x\overline{x} denotes the string obtained by toggling the case of each character in xx (uppercase to lowercase and vice versa).
  • Then, delete the ll-th through rr-th characters of SS and insert TT at the position where the deletion occurred.

Refer to the sample inputs and outputs for clarification.

It can be proved that it is possible to remove all (s and )s from the string using the above operations, and the final string is independent of how and in what order the operations are performed.
Determine the final string.

What does it mean that the parentheses in SS are properly matched? First, a correct parenthesis sequence is defined as follows:

  • A correct parenthesis sequence is a string that satisfies one of the following conditions:

  • It is an empty string.

  • There exists a correct parenthesis sequence AA, and the string is formed by concatenating (, AA, ) in this order.

  • There exist non-empty correct parenthesis sequences AA and BB, and the string is formed by concatenating AA and BB in this order.

The parentheses in SS are properly matched if and only if the (s and )s extracted from SS without changing the order form a correct parenthesis sequence.

Constraints

  • 1S5×1051 \le |S| \le 5 \times 10^5
  • SS consists of uppercase and lowercase English letters, (, and ).
  • The parentheses in SS are properly matched.

Input

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

SS

Output

Print the final string.

Sample Input 1

((A)y)x

Sample Output 1

YAx

Let us perform the operations for S=S = ((A)y)x.

  • Choose l=2l=2 and r=4r=4. The substring (A) is removed and replaced with a.
    • After this operation, S=S = (ay)x.
  • Choose l=1l=1 and r=4r=4. The substring (ay) is removed and replaced with YA.
    • After this operation, S=S = YAx.

After removing the parentheses, the string becomes YAx, which should be printed.

Sample Input 2

((XYZ)n(X(y)Z))

Sample Output 2

XYZNXYZ

Let us perform the operations for S=S = ((XYZ)n(X(y)Z)).

  • Choose l=10l=10 and r=12r=12. The substring (y) is removed and replaced with Y.
    • After this operation, S=S = ((XYZ)n(XYZ)).
  • Choose l=2l=2 and r=6r=6. The substring (XYZ) is removed and replaced with zyx.
    • After this operation, S=S = (zyxn(XYZ)).
  • Choose l=6l=6 and r=10r=10. The substring (XYZ) is removed and replaced with zyx.
    • After this operation, S=S = (zyxnzyx).
  • Choose l=1l=1 and r=9r=9. The substring (zyxnzyx) is removed and replaced with XYZNXYZ.
    • After this operation, S=S = XYZNXYZ.

After removing the parentheses, the string becomes XYZNXYZ, which should be printed.

Sample Input 3

(((()))(()))(())

Sample Output 3

The final outcome may be an empty string.

Sample Input 4

dF(qT(plC())NnnfR(GsdccC))PO()KjsiI((ysA)eWW)ve

Sample Output 4

dFGsdccCrFNNnplCtQPOKjsiIwwEysAve