#P5284. [十二省联考 2019] 字符串问题
[十二省联考 2019] 字符串问题
Description
You are given a string .
Tiffany will select substrings as type strings. The -th one () is .
Similarly, Yazid will select substrings as type strings. The -th one () is .
Additionally, you are given dominance relations. Each relation states that the -th type string dominates the -th type string.
Find a target string with the maximum possible length, such that there exists a partition of as () satisfying:
- Every string in the partition is a type string: that is, there exists a type string equal to it. WLOG, assume .
- For every pair of adjacent strings (), there exists a type string dominated by , such that this type string is a prefix of .
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 , there exists a string satisfying the constraints whose length is greater than ), output .
Input Format
Each test file contains multiple datasets. The first line contains a non-negative integer indicating the number of datasets. Then each dataset is described as follows:
- Line : a string consisting only of lowercase letters.
- Line : a non-negative integer , the number of type strings. The next lines each contain two integers separated by a space.
- In this part, the two integers on line are , , describing the -th type string.
- It is guaranteed that .
- Next line: a non-negative integer , the number of type strings. The next lines each contain two integers separated by a space.
- In this part, the two integers on line are , , describing the -th type string.
- It is guaranteed that .
- Next line: a non-negative integer , the number of dominance relations. The next lines each contain two integers separated by a space.
- The two integers , on each line describe a dominance relation ; see 【Description】 for details.
- It is guaranteed that , . All dominance relations are pairwise distinct, i.e., there do not exist two relations with both the same and the same .
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 .
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 , the type strings are and , the type string is , and dominates . We can find the string , which can be split into , and has (dominated by ) as a prefix. It can be proven that no longer string satisfying the constraints exists.
For dataset , the only difference from dataset is that the only type string is . It is easy to prove that an infinitely long string satisfying the constraints exists.
For dataset , it is easy to prove that is the longest string satisfying the constraints.
Subtasks
| Test Point | Other Constraints | |||||
|---|---|---|---|---|---|---|
| Guaranteed | Guaranteed that all | |||||
| None | ||||||
| Guaranteed that all | ||||||
| None | ||||||
| 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, means that the length of any type string does not exceed the length of any type string.
For all test points, it is guaranteed that , and within each test point, across all datasets, the totals of , , , and will each not exceed times the per-dataset limit for that test point. For example, for test point , we have , etc. In particular, for test point , we require .
For every dataset in all test points, it is guaranteed that: , , .
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
京公网安备 11011102002149号