QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 1024 MB Total points: 100

#15047. No Game No Life

Statistics

Game of Life

XDD is a game enthusiast who is addicted to games all day long. To save him from his addiction, his father decided to teach him some knowledge by designing a game.

One day, XDD's father designed the following game.

Given $N$ strings consisting of lowercase letters, followed by $Q$ queries. Each query consists of two strings $s_1$ and $s_2$. For each query, find the $K$-th smallest cost in the process of the two strings "meeting".

The definition of "meeting" is as follows: Suppose the longest common prefix of two strings $s_1$ and $s_2$ is $T$. Then, repeatedly remove one character from the end of $s_1$ and $s_2$ respectively until both strings are equal to $T$. The cost of removing a character $c$ from the end of a string is $\text{pow}(c, \text{num}[c]) \pmod{100007}$, where $c$ is the result of the character minus 'a' plus 1, and $\text{num}[c]$ is the number of occurrences of this character in the entire current string.

For example, if the two strings are "aabbcc" and "aabddd", the sequence of costs for them to meet is: aabbcc -> aabbc (cost $3^3=27 \pmod{100007} = 27$, note: the example in the problem description uses $3*3=9$, please follow the formula $\text{pow}(c, \text{num}[c]) \pmod{100007}$) -> aabb (cost $3^1=3$) -> aab (cost $2^1=2$) aabddd -> aabdd (cost $4^3=64$) -> aabd (cost $4^2=16$) -> aab (cost $4^1=4$) The costs in ascending order are: 2, 3, 4, 16, 27, 64.

Input

The first line contains $N$ and $Q$. The next $N$ lines each contain a string, representing $s[i]$. Then $Q$ lines follow, each containing three integers $x, y, k$, representing a query for the $k$-th smallest cost for the $x$-th string and the $y$-th string to meet. If the $k$-th smallest cost does not exist, output -1.

Output

For each query, output one integer representing the answer.

Examples

Input 1

5 5
oooono
ooono
oono
ono
no
1 2 5
2 3 4
3 4 3
4 5 2
1 5 4

Output 1

59326
3375
225
14
15

Constraints

Case $n$ $q$ MAXLEN $\sum \text{LEN}$ Additional
1 $\le 1000$ $\le 1000$ $\le 1000$ /
2 $\le 1000$ $\le 100000$ $\le 1000$ $\le 1000000$ It is guaranteed that there exists a permutation of the given strings $s[1] \dots s[n]$ such that $\sum (\text{len}(s[i]) - f(s[i], s[i+1])) \le 1000000$, where $f(s[i], s[i+1])$ denotes the length of the longest common prefix of the two strings.
3 $\le 1000$
4 $\le 1000$
5 $\le 1000$
6 $\le 100000$
7 $\le 100000$
8 $\le 100000$
9 $\le 100000$ $\le 10000000$
10 $\le 100000$

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.