luogu#P8227. 「Wdoi-5」建立与摧毁的结界
「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 be a nested parentheses sequence. Here is the 0-th order nested parentheses sequence, defined as a single pair of brackets ; and for , is the -th order nested parentheses sequence, defined as , i.e. adding one layer of brackets outside .
- Let be a tiled parentheses sequence. Here is the 0-th order tiled parentheses sequence, defined as a single pair of brackets ; and for , is the -th order tiled parentheses sequence, defined as , i.e. adding one pair of brackets to the left of .
For example, is , and is .
Now Ran gives two valid parentheses sequences and , both of length . Chen can transform sequence using the following operations:
- Choose any non-negative integer , choose a substring of the parentheses sequence that is a -th order nested parentheses sequence, and then change it into a -th order tiled parentheses sequence.
- Choose any non-negative integer , choose a substring of the parentheses sequence that is a -th order tiled parentheses sequence, and then change it into a -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 into sequence . It can be proven that there is always an operation plan that can transform into .
Input Format
- The first line contains one integer , representing the length of the parentheses sequences and .
- The next line contains characters describing parentheses sequence . It is guaranteed that sequence is valid.
- The next line contains characters describing parentheses sequence . It is guaranteed that sequence is valid.
Output Format
- One line containing one integer, representing the minimum number of steps needed to convert into (it may be , meaning no operation is performed).
14
((()())(()()))
()()()()()()()
6
14
((()())(()()))
(()())(()())()
10
Hint
Sample is in the attached file .
Sample is in the attached file . It satisfies special property (see below).
Sample is in the attached file .
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:
- is a valid parentheses sequence.
- If is a valid parentheses sequence, then is a valid parentheses sequence.
- If both and are valid parentheses sequences, then is a valid parentheses sequence.
We say is a substring of if and only if we delete some characters at the beginning of (possibly deleting none), and then delete some characters at the end of (possibly deleting none), and the remaining character sequence is exactly the same as .
Constraints and Notes
This problem has test points, with 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 : it is guaranteed that is a -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