#P17214. [ICPC 2017 Nanning R] Banned Patterns

    ID: 19639 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>字符串2017AC 自动机ICPC哈希表

[ICPC 2017 Nanning R] Banned Patterns

Problem Description

:::align{center} :::

The new censorship law has been passed and a mass of characters and strings are explicitly prohibited appearing in the public publication.

Specifically, NN patterns were blacklisted. To simplify the problem, we only consider the uppercase letters and each pattern is a string that consists of only uppercase letters.

The authority makes a total ban on those strings which have at least one substring matching a banned pattern.

If a substring can be transmuted to a pattern by choosing a permutation of all 2626 uppercase letters, the matching satisfies the correctness. Here we list some examples.

Each two of ABBABB, ACCACC, BAABAA, UTTUTT, XZZXZZ and ZAAZAA match each other.

ABCDECBAABCDECBA matches TYQAXQYTTYQAXQYT but does not match QWEWSEWQQWEWSEWQ.

You aim to design an efficient algorithm to judge the validity of upcoming publications.

Input Format

The first line contains a number T(1T20)T (1 \le T \le 20) indicating the number of test cases.

For each test case, the first line is the number of banned patterns N(1N5000)N (1 \le N \le 5000).

Each of the following NN lines contains a pattern in capital letters.

The (N+2)(N + 2)-th line contains an integer M(1M250000)M (1 \le M \le 250000) which is the number of strings asked to be judged based on above patterns.

Each of the following MM lines consists a string in capital letters as well.

The total length of patterns in a test case should not be larger than 100000100000, and the total length of strings asked to be judged should not be larger than 10000001000000.

Output Format

For each test case, output a line with the index and MM letters. Then for each given string, output “Y” if it is prohibited, or “N” if it is allowed.

2
2
AAB
ABB
8
TUU
ZZY
UVW
ABABABA
UUVV
TTUUO
EEEE
XY
1
ABCBD
5
UVWXY
UVVVY
UVWVY
YVWVY
VVWVY
Case #1: Y Y N N Y Y N N
Case #2: N N Y N N