#P15994. [PA 2026] 归并括号序列 / Splatanie nawiasów
[PA 2026] 归并括号序列 / Splatanie nawiasów
Background
$\large{\bf{{Warning: abusing the judging for this problem, even once, will result in a ban.}}}$
Problem Description
We define a merge of strings and as any string obtained by interleaving the characters of and . In other words, after merging, the characters can be colored using two colors such that reading only the characters of one color gives exactly string , and reading only the characters of the other color gives exactly string .
A string consisting of left parentheses and right parentheses is called a valid bracket expression if the number of left parentheses in equals the number of right parentheses, and in every prefix of , the number of left parentheses is at least the number of right parentheses.
You are given two bracket strings and . Compute how many pairs satisfy the following: there exists a valid bracket expression such that is a merge of string and string (i.e., the non-empty substring of from position to position ).
Input Format
The first line contains string , and the second line contains string .
To avoid excessively large input, each string is given in the following form:
Each line starts with an integer (), followed by a character (either or ), and then a sequence of integers (). The string encoded in this way starts with character repeated times, then the other type of parenthesis repeated times, then character repeated times, and so on.
Output Format
Output one integer: the number of pairs that satisfy the condition, i.e., the number of pairs such that some merge of string and substring is a valid bracket expression.
3 ( 1 3 1
3 ) 1 3 2
3
2 ( 1 1
4 ) 2 1 1 2
4
Hint
Sample 1 explanation: The strings described in this sample are and . From the second string, we can take the substrings , , or .
In the first case, one valid merge of string and substring is $\colorbox{DDDDDD}{\texttt{(}}\colorbox{BBBBBB}{\texttt{)(((}}\colorbox{DDDDDD}{\texttt{)))(}}\colorbox{BBBBBB}{\texttt{)}}$.
Sample 2 explanation: The strings described in this sample are and . Note that although the substring from the second character to the third character and the substring from the fourth character to the fifth character are the same substring , we still count it twice. Although the string itself is a valid bracket expression, the empty substring is not included in the count.
Translated by ChatGPT 5