Kontakt for Two
Time limit2sMemory limit512 MB
Given a dictionary and queries of (secret word, K), find the maximum number of words the guesser can name before the game ends.
- Level
Hard9 of 10
- Topics
- Trie, Tree, Dynamic programming, String
- Solved
- No attempts yet
Statement
Elk Valera loves playing Kontakt, a popular word game. Lately he rarely gets to play it, because the game needs at least three players, and gathering three people is hard with limited time and mismatched schedules.
So Valera invented a two-player version called Kontakt for Two. One player picks a non-empty word and reveals its first letter. The guesser names words that start with that letter, one after another, and the first player says whether each word is correct. The guesser may not repeat a word.
If the guesser names words, where is a fixed number, without guessing the word, the first player reveals the second letter. From then on, the guesser may name only words that start with these two letters and that they have not named before. This holds even when the guesser knew only one letter. If, after the second letter appears, the guesser can name another words, the first player reveals the third letter, and so on.
If the first player has revealed every letter and the guesser then names words starting with the word without naming it, the first player states that the revealed letters form the word, and the game ends. The game also ends when the guesser names the word.
To make the game more interesting, Valera, playing the first player, picks a word that makes the guesser make as many attempts as possible. A correct attempt also counts. The course of the game depends on the guesser's vocabulary and on the chosen . Valera knows his friends well, so he can list every word each of them knows. He always picks a word the guesser knows. Choosing the word and is not easy.
Valera wants a program that, given a dictionary and pairs of (secret word, ), finds the maximum number of words the guesser can name before the game ends.
Input
The first line contains a single integer , the number of words in the dictionary of the potential guesser. Each of the next lines contains one non-empty word of lowercase English letters. The total length of all words does not exceed .
Words may be identical in spelling. For example, in Russian, замОк and зАмок are different words with the same spelling, because the stress differs. In the language Valera's group plays in, many accent patterns exist, so identically spelled words can be common. During the game, players tell such words apart by how they pronounce them.
The line contains (), the number of queries. Each of the next lines contains two integers and (): the index of the secret word in the dictionary and the value of . Words are numbered from 1 in input order.
Output
For each query, print on a separate line one integer: the maximum number of words the guesser can name before the game ends by a correct guess or by the first player revealing the word.