Spelling with Chemistry

시간 제한1초메모리 제한2048 MB

요약
길이 1에서 5까지인 기호 200개 이하가 주어질 때, 단어 20개 이하를 그 기호들의 나열로 나누는 경우의 수를 각각 센다.
난이도

보통10점 중 4점

유형
동적 계획법, 문자열, 해시맵
정답자
아직 제출이 없습니다

문제

Each element on the periodic table has a one or two letter symbol. These symbols can be combined to spell words, and many times there are multiple ways of spelling a word using different combinations of symbols. Can you determine how many different ways a word can be spelled using the symbols?

For example, the word "bacon" can be spelled 33 different ways:

  1. Barium, Cobalt, Nitrogen: BaCoN
  2. Boron, Actinium, Oxygen, Nitrogen: BAcON
  3. Barium, Carbon, Oxygen, Nitrogen: BaCON

However, your friend from another universe wants to spell words with the elements found there, which are different than the elements in our universe. So, your input will include the set of symbols in the periodic table for that particular universe. In this other universe, they don't follow the same naming convention that we do, so the provided symbols could be up to 55 letters in length.

입력

The first line of input will contain an integer NN (1≤N≤2001 \leq N \leq 200), the number of symbols in that universe.

The next NN lines will contain a single unique symbol, each between 11 and 55 letters in length.

The next line of input will contain an integer MM (1≤M≤201 \leq M \leq 20), the number of words for which you should determine how many ways they can be spelled.

The next MM lines each contain a single word, each between 11 and 4040 letters in length, containing only lowercase letters.

출력

Output a total of MM lines, each line indicating the number of ways that the corresponding input word can be spelled using the symbols in the periodic table for that universe. If a word is impossible to spell, output a 00 on that line.

예제1

  1. 예제 1

    입력
    13
    Ac
    As
    B
    Ba
    C
    Co
    H
    O
    P
    N
    S
    Th
    Y
    3
    bacon
    bash
    python
    
    예상 출력
    3
    2
    1