This page is still under construction.

Parts of this page are still being built. What you see may change.

Cow Typing

Time limit1sMemory limit128 MB

Summary
Given a dictionary and short email words, simulate the cow's letter selector with a trie and count total button presses, including circular highlight moves and prints.
Level

Medium5 of 10

Topics
Trie, String, Simulation, Implementation
Solved
No attempts yet

Problem

The cows have discovered e-mail and love receiving messages from Farmer John. Replying is hard, though: with their hooves, cows struggle to use a normal keyboard. So Farmer John built a special cow input system.

The system has four buttons — previous, next, add, and print — and a display. Instead of typing letters directly, a cow selects letters one at a time and appends them to build a word. Cows only ever write in capital letters.

Every word a cow types comes from a fixed dictionary of known words (given in the input). No dictionary word is longer than 20 characters.

The display has two lines:

  • the word built so far, and
  • the list of possible next letters, with exactly one letter highlighted.

The next and previous buttons move the highlight forward or backward through this list. The list is circular: pressing next on the last letter wraps to the first, and pressing previous on the first wraps to the last.

The list of possible next letters only ever contains letters that could still lead to a dictionary word, given what has been typed so far. For example, if no dictionary word starts with BD, then after typing B the letter D never appears in the list. When the word so far is empty, the list contains the first letters of every dictionary word.

Whenever the highlighted letter is added, it is appended to the current word and the list of possible next letters is recomputed immediately. The highlight then resets to the alphabetically first letter of the new list.

For example, suppose the dictionary is ACE, APPLE, BANANA, PEAR. The list starts as A B P with A highlighted. If A is added, the list becomes C P with C highlighted (the earliest in the alphabet).

Pressing print appends the finished word to the e-mail; the display then resets and the process starts over for the next word.

Using the four-word dictionary above, here is the sequence to type the word APPLE (*A* means A is highlighted):

Action       WORD          Possible letters
[initial]    _________     *A* B P
ADD          A________     *C* P
NEXT         A________     C *P*
ADD          AP_______     *P*
ADD          APP______     *L*
ADD          APPL_____     *E*
ADD          APPLE____
PRINT        _________     *A* B P

Typing APPLE this way takes 7 button presses.

Because a cow always presses as few buttons as possible, moving the highlight from its current position to a target letter costs the smaller of the two circular distances — going forward with next or backward with previous.

Given the dictionary and a short e-mail, report the total number of button presses (moves, adds, and prints) needed to type every word of the e-mail.

Input

  • Line 1: an integer DD (1≤D<250001 \le D < 25000), the number of words in the dictionary.
  • The next DD lines: one dictionary word per line. Every word consists of uppercase letters A–Z, has length between 1 and 20, and all DD words are distinct.
  • The next line: an integer NN (1≤N≤201 \le N \le 20), the number of words in the e-mail.
  • The next NN lines: one word per line, each a word to type. Every such word is guaranteed to appear in the dictionary.

Output

  • Print a single integer: the total number of button presses required to type the whole e-mail, including one print press after each word.

Examples2

  1. Example 1

    Input
    4
    ACE
    APPLE
    BANANA
    PEAR
    1
    APPLE
    
    Expected output
    7
    
  2. Example 2

    Input
    4
    ACE
    APPLE
    BANANA
    PEAR
    4
    ACE
    APPLE
    BANANA
    PEAR
    
    Expected output
    25