Alpha of Degree k
Time limit1sMemory limit128 MB
Given a dictionary, answer queries asking for the shortest chain from word s to word t where each step shares a suffix-prefix overlap of length at least k, with a cap on chain length.
- Level
Hard8 of 10
- Topics
- Graph, BFS, String matching, String
- Solved
- No attempts yet
Problem
Given two strings and , we say that the relation "alpha of degree ", written , holds between and if and only if there exists a suffix of of length at least that is also a prefix of . The alpha relation is not defined for .
For example, "telnet" is -related to "network" (the suffix "net" of "telnet" is a prefix of "network"), while "block" is -related (and therefore also -, -, and -related) to "locker" (the suffix "lock" of "block" is a prefix of "locker").
An chain of length (with ) from a word to a word is a list of words in which is the first word, is the last word, and the relation holds between every pair of consecutive words. For example, the following is an chain of length from "cartoon" to "manual":
You are given a dictionary of words together with several queries. Each query gives two words and (both taken from the dictionary) and two integers and . For each query, decide whether there is an chain from to that uses only words from and whose length does not exceed ; if so, report the length of the shortest such chain.
Input
The first line contains a single integer , the number of test cases.
Each test case begins with a line containing two integers and , where is the number of words in this test case's dictionary and is the number of queries. It is guaranteed that and .
The next lines each contain one dictionary word. Every word consists only of lowercase letters, contains no spaces, is at most characters long, and no word appears more than once.
Each of the following lines describes one query: two words and (both from the dictionary) and two integers and , separated by single spaces.
Output
For each query, print exactly one line.
Let be the test case number (starting from ) and let be the query number within that test case (also starting from ).
If there is no chain from to that uses only dictionary words and has length at most , print:
a.b none
Otherwise, let be the length (the number of consecutive-word steps) of the shortest such chain, and print:
a.b c
The value is uniquely determined, so exactly one output is correct for each query.