luogu#P8227. 「Wdoi-5」建立与摧毁的结界

    ID: 7235 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心递归洛谷原创树形 DP洛谷月赛双指针 two-pointer

「Wdoi-5」建立与摧毁的结界

Background

Yukari Yakumo has the ability to manipulate boundaries. Ran Yakumo, as her shikigami, also has some ability to operate boundaries. However, Chen, as Ran Yakumo's shikigami, is still not skilled enough and cannot interfere with boundaries too much.

So Ran Yakumo plans to teach Chen how to use boundaries. A boundary can be abstracted as layers of parentheses, and operating a boundary is essentially modifying a parentheses sequence. Since Chen's ability is still limited, she can only perform some simple building and destroying of boundaries.

Even so, with simple operations, one boundary can be transformed into another. As practice for Chen, Ran found two boundary templates. The difficulty of transforming one boundary into the other is the minimum number of times Chen needs to use her ability. But because the boundaries are too long, Ran decides to write a program (?) to help her compute the boundary difficulty.

Problem Description

A boundary can be abstracted as a parentheses sequence consisting of round brackets. Define the following:

  • Let DiD_i be a nested parentheses sequence. Here D0D_0 is the 0-th order nested parentheses sequence, defined as a single pair of brackets ()\verb!()!; and for k1k \geq 1, DkD_k is the kk-th order nested parentheses sequence, defined as (Dk1)\verb!(!D_{k-1}\verb!)!, i.e. adding one layer of brackets outside Dk1D_{k-1}.
  • Let FiF_i be a tiled parentheses sequence. Here F0F_0 is the 0-th order tiled parentheses sequence, defined as a single pair of brackets ()\verb!()!; and for k1k \geq 1, FkF_k is the kk-th order tiled parentheses sequence, defined as ()Fk1\verb!()!F_{k-1}, i.e. adding one pair of brackets to the left of Fk1F_{k-1}.

For example, ((()))\verb!((()))! is D2D_2, and ()()()()\verb!()()()()! is F3F_{3}.

Now Ran gives two valid parentheses sequences AA and BB, both of length nn. Chen can transform sequence AA using the following operations:

  • Choose any non-negative integer kk, choose a substring of the parentheses sequence that is a kk-th order nested parentheses sequence, and then change it into a kk-th order tiled parentheses sequence.
  • Choose any non-negative integer kk, choose a substring of the parentheses sequence that is a kk-th order tiled parentheses sequence, and then change it into a kk-th order nested parentheses sequence.

The definitions of "valid parentheses sequence" and "substring" are given in the Hint section.

Now you need to find the minimum number of operations required to transform sequence AA into sequence BB. It can be proven that there is always an operation plan that can transform AA into BB.

Input Format

  • The first line contains one integer nn, representing the length of the parentheses sequences AA and BB.
  • The next line contains nn characters describing parentheses sequence AA. It is guaranteed that sequence AA is valid.
  • The next line contains nn characters describing parentheses sequence BB. It is guaranteed that sequence BB is valid.

Output Format

  • One line containing one integer, representing the minimum number of steps needed to convert AA into BB (it may be 00, meaning no operation is performed).
14
((()())(()()))
()()()()()()()
6
14
((()())(()()))
(()())(()())()
10

Hint

Sample 33 is in the attached file border3.in/border3.ans\textbf{\textit{border3.in/border3.ans}}.
Sample 44 is in the attached file border4.in/border4.ans\textbf{\textit{border4.in/border4.ans}}. It satisfies special property A\text{A} (see below).
Sample 55 is in the attached file border5.in/border5.ans\textbf{\textit{border5.in/border5.ans}}.

Explanation for Sample 1

  • Step 1: $\texttt{((\underline{()()})(()()))}\to\texttt{((\underline{(())})(()()))}$.
  • Step 2: $\texttt{(((()))(\underline{()()}))}\to\texttt{(((()))(\underline{(())}))}$.
  • Step 3: $\texttt{(((()))\underline{((()))})}\to\texttt{(((()))\underline{()()()})}$.
  • Step 4: $\texttt{(\underline{((()))}()()())}\to\texttt{(\underline{()()()}()()())}$.
  • Step 5: $\texttt{(\underline{()()()()()()})}\to\texttt{(\underline{(((((())))))})}$.
  • Step 6: $\texttt{\underline{((((((()))))))}}\to\texttt{\underline{()()()()()()()}}$.

It can be proven that there is no shorter plan.

Hint

A valid parentheses sequence is defined in the following form:

  • ()\verb!()! is a valid parentheses sequence.
  • If AA is a valid parentheses sequence, then (A)\verb!(!A\verb!)! is a valid parentheses sequence.
  • If both AA and BB are valid parentheses sequences, then ABAB is a valid parentheses sequence.

We say AA is a substring of BB if and only if we delete some characters at the beginning of BB (possibly deleting none), and then delete some characters at the end of BB (possibly deleting none), and the remaining character sequence is exactly the same as AA.

Constraints and Notes

This problem has 2020 test points, with 55 points per test point. The final score is the sum of the scores of all test points.

$$\def\arraystretch{1.5} \begin{array}{|c|c|c|}\hline \textbf{Task} & \bm{n\le } & \textbf{Special property} \cr\hline 1\sim 4 & 10 & - \cr\hline 5\sim 7 & 2\times 10^3 & \text{A} \cr\hline 8\sim 10 & 2\times 10^3 & - \cr\hline 11\sim 15 & 10^6 & \text{A} \cr\hline 16\sim 20 & 10^6 & - \cr\hline \end{array}$$

Special property A\textbf{A}: it is guaranteed that BB is a (n÷21)(n\div 2-1)-th order tiled parentheses sequence.

Friendly Reminder

Considering that contestants may read strings in different ways, it is guaranteed that the input file has no extra spaces at the end of each line, and each line ends with \n (that is, there will be no extra \r).

Translated by ChatGPT 5