#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 2626 lowercase Latin letters. Since the number of regions may exceed 2626, 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 c\tt c, its superior is b\tt b, the superior of b\tt b is a\tt a, and a\tt a has no superior, then this region is described as abc\tt abc. Clearly, this description also contains the descriptions of c\tt c’s superior b\tt b and b\tt b’s superior a\tt a, which are ab\tt ab and a\tt a, 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 nn 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 mm pairs of people visiting this country. For each pair, the first person likes the jj-th region in the ii-th description; let this region’s description be s1s_1. The second person likes the ll-th region in the kk-th description; let this region’s description be s2s_2. To unify their itinerary, they decide to visit the region whose description is ss (obviously, they only care about the region name, not the region itself). Let the length of ss be tt. The string ss must satisfy:

  1. tjt\leq j, tlt\leq l.
  2. s[1t]=s1[jt+1j]s[1\cdots t]=s_1[j-t+1\cdots j], s[1t]=s2[lt+1l]s[1\cdots t]=s_2[l-t+1\cdots l], i.e., ss is a common suffix of the prefix s1[1j]s_1[1\cdots j] and the prefix s2[1l]s_2[1\cdots l].
  3. Maximize tt.

To avoid overly large output, you only need to convert this string into a base-2626 number (as generated below), then convert it to base 1010 and output it modulo (109+7)(10^9+7):

  • a0a \to 0;
  • b1b \to 1;
  • ……
  • z25z \to 25.

For example, region cab\tt cab is encoded as 2×262+0×261+1×260=13532\times26^2+0\times26^1+1\times26^0=1353.

Input Format

The first line contains an integer nn.

Lines 22 to n+1n+1: the (i+1)(i+1)-th line contains a string aia_i, representing the ii-th description.

The next line contains an integer mm.

The next mm lines each contain four integers i,j,k,li,j,k,l, with the meanings consistent with the statement.

Output Format

There are mm 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 11, the common suffixes include ab\tt ab and b\tt b, but there is no region ab\tt ab, only region b\tt b, so they can only choose region b\tt b.

In query 22, the common suffixes include abb\tt abb, bb\tt bb, and b\tt b, but there are no regions abb\tt abb or bb\tt bb, only region b\tt b, so they can only choose region b\tt b.

Constraints and Notes

Let the total number of regions in this country be tottot (note that the total length of input strings may exceed tottot!).

  • For 30%30\% of the testdata, 1tot,m,n1001\le tot, m, n \le 100.
  • For 50%50\% of the testdata, 1tot,m,n10001\le tot, m, n \le 1000.
  • For 80%80\% of the testdata, 1tot,m,n1051\le tot, m, n \le 10^5.
  • For 100%100\% of the testdata, 1tot,m,n1061\le tot, m, n \le 10^6.

It is guaranteed that the input file does not exceed 20MB20\text{MB}.

HEOI2012 Day 2 Task 2.

Translated by ChatGPT 5