Under construction

Still being built. Things may change

Avant-garde Autocorrect

Interview

Time limit1sMemory limit1024 MB

Summary
Given a frequency-ranked dictionary and a message, find the fewest key presses per word when autocomplete with tab, backspace, and single letters are allowed.
Level

Medium6 of 10

Topics
Trie, Dynamic programming, String
Solved
No attempts yet

Problem

Filip and Luka found their purpose in life in a WhatsApp group. Both want to entertain the rest of the group, but their methods are completely different.

Filip is a calculating type. Every message he sends is carefully chosen, and it usually makes at least one other group member smile. Sometimes he laughs at himself.

Luka is a very eloquent and well-read entertainer. His tactic is to retell many funny and embarrassing stories and amuse the crew with a technique called poistovječivanje sa situacijama, which means identifying with situations.

Luka can type only about 60 words per minute, so he sometimes cannot fully use his technique. He decided to install the most avant-garde version of autocorrect that the iPhone offers.

Luka's autocorrect has a dictionary of n words sorted by how often they are used. Whenever Luka types a word, autocorrect suggests the most frequently used word that starts with all the letters typed so far, if such a word exists. Pressing the tab key completes the suggested word, and Luka can keep typing afterward if needed. Autocorrect can be used only after at least one letter has been typed.

Luka wants to type a message of m words. For each word, he wants to know the minimum number of key presses needed to write it. Besides the tab key, Luka can use backspace and one key for each individual letter.

Help Luka calculate the minimum number of key presses needed for each word.

Input

The first line contains natural numbers nn and mm (1≤n≤100 0001 \le n \le 100\,000, 1≤m≤100 0001 \le m \le 100\,000).

The next n lines contain the words that autocorrect knows, in order of frequency, with the most frequently used word first.

The next m lines contain the words Luka wants to type.

All words consist of lowercase letters of the English alphabet. The input file is smaller than 1 MB.

Output

Print m lines. For each word in the message, print the minimum number of key presses Luka needs to type it.

Examples2

  1. Example 1

    Input
    5 5
    austria
    autocorrect
    program
    programming
    computer
    autocorrelation
    programming
    competition
    zyx
    austria
    
    Expected output
    12
    4
    11
    3
    2
    
  2. Example 2

    Input
    5 3
    yogurt
    you
    blessing
    auto
    correct
    bless
    you
    autocorrect
    
    Expected output
    5
    3
    9