#P4600. [HEOI2012] 旅行问题
[HEOI2012] 旅行问题
Description
yz is the leader of Country Z. He requires that the name of each region can only be one of the lowercase Latin letters. Since the number of regions may exceed , a problem arises: how can we tell regions with the same name apart? Therefore, yz specifies that the description of a region must include all of its superiors, and the superiors must be listed in order. Thus, a region’s description is a string. For example, if a region’s name is , its superior is , the superior of is , and has no superior, then this region is described as . Clearly, this description also contains the descriptions of ’s superior and ’s superior , which are and , respectively.
Note that each region has at most one superior. Among regions with the same superior, their names are different. Among regions with no superior, their names are also different.
Now, yz has published the descriptions of regions to the public. These descriptions include the descriptions of all regions in Country Z, and you are asked to handle visitors’ travel problems.
There are pairs of people visiting this country. For each pair, the first person likes the -th region in the -th description; let this region’s description be . The second person likes the -th region in the -th description; let this region’s description be . To unify their itinerary, they decide to visit the region whose description is (obviously, they only care about the region name, not the region itself). Let the length of be . The string must satisfy:
- , .
- , , i.e., is a common suffix of the prefix and the prefix .
- Maximize .
To avoid overly large output, you only need to convert this string into a base- number (as generated below), then convert it to base and output it modulo :
- ;
- ;
- ……
- .
For example, region is encoded as .
Input Format
The first line contains an integer .
Lines to : the -th line contains a string , representing the -th description.
The next line contains an integer .
The next lines each contain four integers , with the meanings consistent with the statement.
Output Format
There are lines. Each line contains one integer, which is the encoding of the answer string.
2
aabb
babb
2
1 3 2 3
1 4 2 4
1
1
Hint
Sample Explanation
In query , the common suffixes include and , but there is no region , only region , so they can only choose region .
In query , the common suffixes include , , and , but there are no regions or , only region , so they can only choose region .
Constraints and Notes
Let the total number of regions in this country be (note that the total length of input strings may exceed !).
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
It is guaranteed that the input file does not exceed .
HEOI2012 Day 2 Task 2.
Translated by ChatGPT 5
京公网安备 11011102002149号