Abbreviations

Time limit1sMemory limit128 MB

Summary
Given ignored stopwords, an abbreviation, and a sentence, count the distinct ways to split the abbreviation into pieces matched as subsequences of the meaningful words in order.
Level

Medium7 of 10

Topics
Dynamic programming, String, Combinatorics, Implementation
Solved
No attempts yet

Problem

Acronyms are often formed more flexibly than just taking the first letter of each word. For example, GDB stands for Gnu DeBugger: the letters are taken from the words in order, and a single word (DeBugger) may contribute several letters.

We form abbreviations by the following rules:

  1. Meaningless words (such as of, a, the) are ignored.
  2. The letters taken from each word must appear in that word's left-to-right order.
  3. Every meaningful word must be used — each contributes at least one letter.

Concretely, after deleting the meaningless words, let the remaining meaningful words, in their original order, be w1,w2,…,wkw_1, w_2, \dots, w_k. The abbreviation must be split into kk consecutive non-empty pieces p1p2…pkp_1 p_2 \dots p_k whose concatenation is the whole abbreviation, where each piece pip_i is a subsequence of word wiw_i. Letters are matched case-insensitively (the abbreviation is uppercase, the words are lowercase).

Not every real abbreviation obeys these rules. For instance, RADAR stands for "RAdio Detecting And Ranging"; because it draws a letter from the ignored word "and", it cannot be formed under these rules.

Given the list of meaningless words, an abbreviation, and a sentence, count the number of different ways the abbreviation can be formed. Two ways are different if the abbreviation is split between the words differently, or if any letter is taken from a different position within its word.

Input

The input consists of several test cases. The first line of each test case contains an integer nn (1≤n≤1001 \le n \le 100), the number of meaningless words. Each of the next nn lines contains one meaningless word in lowercase.

After that come one or more query lines. Each query line contains an uppercase abbreviation followed by a lowercase sentence (lowercase words separated by spaces). The abbreviation has length at least 1, and the sentence contains at least one meaningless word. Every abbreviation and every sentence is at most 150 characters long. The list of queries ends with a line containing exactly LAST CASE.

The input ends with a test case whose first line is 0.

Output

For each query, if the abbreviation cannot be formed, print

<abbreviation> is not a valid abbreviation

otherwise print

<abbreviation> can be formed in i ways

where i is the number of ways to form it. The value i fits in a 32-bit signed integer.

Examples1

  1. Example 1

    Input
    2
    and
    of
    ACM academy of computer makers
    RADAR radio detection and ranging
    LAST CASE
    2
    a
    an
    APPLY an apple a day
    LAST CASE
    0
    
    Expected output
    ACM can be formed in 2 ways
    RADAR is not a valid abbreviation
    APPLY can be formed in 1 ways