The longest common prefix of two words is the longest word that both words start with. For example,
the longest common prefix of the words identity
and idealistic
is the word ide
.
A database contains
The algorithm to search for a query word
Analysing the algorithm shows that the number of steps needed to find a word
Write a program that calculates the number of steps the algorithm uses to find each of the
Input Specification
The first line contains an integer
Each of the following
The following line contains an integer
Each of the following
All words in the input will be strings of less than
Output Specification
Output one integer per line for each query word, the number of steps the algorithm uses when searching for the word.
Sample Input 1
5
hobotnica
robot
hobi
hobit
robi
4
robi
hobi
hobit
rakija
Sample Output 1
12
10
16
7
Sample Input 2
8
majmunica
majmun
majka
malina
malinska
malo
maleni
malesnica
3
krampus
malnar
majmun
Sample Output 2
8
29
14
In the second example, the number of steps to search for the word krampus
is
When searching for the word malnar
, we need three steps for each of the first three words, and four
steps for each of the remaining five words, for a total of
To find the word majmun
we use a total of majmunica
is longer than
the query word. For the second word, we also have six successful comparisons and a final step in which
it is established that the words are equal. After finding the word, the algorithm terminates with great
joy.
Comments