The Journey of the King

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

요약
서로 다른 단어들의 사전이 주어질 때, 두 카드는 두 연결 순서 중 하나가 사전에 있으면 짝이 되며, 정해진 순서에서 최대 짝 수를 구한다.
난이도

어려움10점 중 8점

유형
트라이, 문자열 매칭, 그래프, 그리디
정답자
아직 제출이 없습니다

문제

You are very close to becoming the King of Games. The only thing left to do is to win in a card game against the incarnation of the King of Nusantara, Anda, whose soul resides inside you as your split personality.

Each player has a deck of cards, each card contains a word. Within each deck, there are no two cards containing the same word. There is also a dictionary consisting of DD distinct words: \[W_1,W_2,…,W_D]\[W\_1, W\_2, \dots , W\_D].

The game consists of NN turns. In turn ii, Anda will play a card with the word A_iA\_i. Then, you can either match his card with one of your remaining cards or skip this turn. Two cards, aa and bb, match if either the words a+ba + b or b+ab + a exist in the dictionary. The operator ++ represents the concatenation operation. For instance, the concatenation of words AU and RA is AU ++ RA == AURA. Once you match a card, you cannot use that card for the rest of the game.

Your deck has MM cards (numbered from 11 to MM); card jj contains word B_jB\_j. You want to maximize the number of turns in which you successfully match Anda’s card.

입력

The first line consists of an integer DD (1≤D≤200,0001 ≤ D ≤ 200\\, 000).

Each of the next DD lines consists of a string W_kW\_k. String W_kW\_k consists of only uppercase English letters. The sum of length of W_kW\_k does not exceed 200,000200\\, 000. It is guaranteed that W_k≠W_k′W\_k \ne W\_{k'} for 1≤k<k′≤D1 ≤ k < k' ≤ D.

The following line consists of an integer NN (1≤N≤100,0001 ≤ N ≤ 100\\, 000).

Each of the next NN lines consists of a string A_iA\_i. String A_iA\_i consists of only uppercase English letters. The sum of length of A_iA\_i does not exceed 100,000100\\, 000. It is guaranteed that A_i≠A_i′A\_i \ne A\_{i'} for 1≤i<i′≤N1 ≤ i < i' ≤ N.

The following line consists of an integer MM (1≤M≤100,0001 ≤ M ≤ 100\\, 000).

Each of the next MM lines consists of a string B_jB\_j. String B_jB\_j consists of only uppercase English letters. The sum of length of B_jB\_j does not exceed 100,000100\\, 000. It is guaranteed that B_j≠B_j′B\_j \ne B\_{j'} for 1≤j<j′≤M1 ≤ j < j' ≤ M.

출력

Output a single integer representing the maximum number of turns you match Anda’s card.

예제4

  1. 예제 1

    입력
    3
    AURA
    AURORA
    LAURA
    3
    RA
    REO
    RORA
    2
    AU
    LAU
    
    예상 출력
    2
    
  2. 예제 2

    입력
    3
    HARTA
    TAHTA
    HARU
    3
    HAR
    TAH
    HA
    3
    TA
    RU
    ARU
    
    예상 출력
    2
    
  3. 예제 3

    입력
    1
    AAA
    3
    A
    AA
    AAA
    2
    A
    AA
    
    예상 출력
    2
    
  4. 예제 4

    입력
    1
    INDONESIA
    1
    NATIONAL
    1
    CONTEST
    
    예상 출력
    0