#P5284. [十二省联考 2019] 字符串问题

[十二省联考 2019] 字符串问题

Description

You are given a string SS.

Tiffany will select nan_a substrings as type AA strings. The ii-th one (1ina1 \leqslant i \leqslant n_a) is Ai=S(lai,rai)A_i = S(la_i, ra_i).

Similarly, Yazid will select nbn_b substrings as type BB strings. The ii-th one (1inb1 \leqslant i \leqslant n_b) is Bi=S(lbi,rbi)B_i = S(lb_i, rb_i).

Additionally, you are given mm dominance relations. Each relation (x,y)(x, y) states that the xx-th type AA string dominates the yy-th type BB string.

Find a target string TT with the maximum possible length, such that there exists a partition of TT as T=t1+t2++tkT = t_1+t_2+· · ·+t_k (k0k \geqslant 0) satisfying:

  • Every string tit_i in the partition is a type AA string: that is, there exists a type AA string equal to it. WLOG, assume ti=Aidit_i = A_{id_i}.
  • For every pair of adjacent strings ti,ti+1t_i, t_{i+1} (1i<k1 \leqslant i < k), there exists a type BB string dominated by AidiA_{id_i}, such that this type BB string is a prefix of ti+1t_{i+1}.

For convenience, you only need to output this maximum length.

In particular, if there exists an infinitely long target string (that is, for any positive integer nn, there exists a string satisfying the constraints whose length is greater than nn), output 1-1.

Input Format

Each test file contains multiple datasets. The first line contains a non-negative integer TT indicating the number of datasets. Then each dataset is described as follows:

  • Line 11: a string SS consisting only of lowercase letters.
  • Line 22: a non-negative integer nan_a, the number of type AA strings. The next nan_a lines each contain two integers separated by a space.
    • In this part, the two integers on line ii are laila_i, raira_i, describing the ii-th type AA string.
    • It is guaranteed that 1lairaiS1 \leqslant la_i \leqslant ra_i \leqslant |S|.
  • Next line: a non-negative integer nbn_b, the number of type BB strings. The next nbn_b lines each contain two integers separated by a space.
    • In this part, the two integers on line ii are lbilb_i, rbirb_i, describing the ii-th type BB string.
    • It is guaranteed that 1lbirbiS1 \leqslant lb_i \leqslant rb_i \leqslant |S|.
  • Next line: a non-negative integer mm, the number of dominance relations. The next mm lines each contain two integers separated by a space.
    • The two integers xx, yy on each line describe a dominance relation (x,y)(x, y); see 【Description】 for details.
    • It is guaranteed that 1xna1 \leqslant x \leqslant n_a, 1ynb1 \leqslant y \leqslant n_b. All dominance relations are pairwise distinct, i.e., there do not exist two relations with both the same xx and the same yy.

Output Format

Output the answer for each dataset in order. For each dataset:

  • Output one integer per line, the maximum length of such a string. In particular, if a string satisfying the constraints can be infinitely long, output 1-1.
3
abaaaba
2
4 7
1 3
1
3 4
1
2 1
abaaaba
2
4 7
1 3
1
7 7
1
2 1
abbaabbaab
4
1 5
4 7
6 9
8 10
3
1 6
10 10
4 6
5
1 2
1 3
2 1
3 3
4 1
7
-1
13

Hint

Explanation of Sample 1

For dataset 11, the type AA strings are aaba\texttt{aaba} and aba\texttt{aba}, the type BB string is aa\texttt{aa}, and A2A_2 dominates B1B_1. We can find the string abaaaba\texttt{abaaaba}, which can be split into A2+A1A_2 + A_1, and A1A_1 has B1B_1 (dominated by A2A_2) as a prefix. It can be proven that no longer string satisfying the constraints exists.

For dataset 22, the only difference from dataset 11 is that the only type BB string is a\texttt{a}. It is easy to prove that an infinitely long string satisfying the constraints exists.

For dataset 33, it is easy to prove that abbaabbaaaabb\texttt{abbaabbaaaabb} is the longest string satisfying the constraints.

Subtasks

nan_a nbn_b S\lvert S\rvert Test Point mm AiBj\lvert A_i\rvert \geq \lvert B_j\rvert Other Constraints
100\leq 100 104\leq 10^4 11 104\leq 10^4 Guaranteed Guaranteed that all Ai,Bj100\lvert A_i\rvert,\lvert B_j\rvert\leq 100
1000\leq 1000 2×105\leq 2\times 10^5 232\sim 3 2×105\leq 2\times 10^5 None
=1=1 2×105\leq 2\times 10^5 44 =nb=n_b
2×105\leq 2\times 10^5 565\sim 6 2×105\leq 2\times 10^5 Guaranteed that all rai+1=lai+1ra_i +1=la_{i+1}
787\sim 8 None
9109\sim 10 Not guaranteed

For easier reading, the test point numbers are placed in the middle of the table. Please pay attention to this.

In the table, Ai>Bj|A_i| > |B_j| means that the length of any type BB string does not exceed the length of any type AA string.

For all test points, it is guaranteed that T100T \leqslant 100, and within each test point, across all datasets, the totals of S|S|, nan_a, nbn_b, and mm will each not exceed 1010 times the per-dataset limit for that test point. For example, for test point 11, we have na10×100=1000\sum n_a \leqslant 10 \times 100 = 1000, etc. In particular, for test point 44, we require T10T \leqslant 10.

For every dataset in all test points, it is guaranteed that: 1S2×1051 \leqslant |S| \leqslant 2 \times 10^5, na,nb2×105n_a, n_b \leqslant 2 \times 10^5, m2×105m \leqslant 2 \times 10^5.

Hint

A friendly reminder from the 12 Provinces Joint Exam problem setters:

There are thousands of testdata, clear the first one.

If you do not clear between multiple tests, you will get zero points and shed two lines of tears.

Translated by ChatGPT 5