#P16827. [AFOI 2025] D.谐音替换

    ID: 18727 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>二分树状数组O2优化哈希 hashing

[AFOI 2025] D.谐音替换

Background

Time flies. That afternoon about “homophone replacement” is still clearly imprinted in little ω\omega's mind.

Back then, he insisted on exact matching: only completely identical substrings could be replaced. However, reality often did not go as hoped, and even tiny differences were enough to ruin all efforts.

Many years later, when little ω\omega opened his linguistics notes again, he gained a new understanding of “homophones”: why demand complete equality? Having the same beginning or the same ending is already “homophonic” enough. Just like bits of memory, it does not need to be fully replayed; only a beginning or an ending is enough to connect the whole story.

Problem Description

Little ω\omega is an algorithm contest participant who likes linguistics. In linguistics, homophone replacement means replacing original words with words that have the same or similar pronunciation. Little ω\omega found that the process of homophone replacement can be described using prefix or suffix relationships of strings. Specifically, little ω\omega defines homophone replacement as the following string problem:

  • String XX is a homophone replacement of YY if and only if XX is a prefix of YY, or XX is a suffix of YY.
  • Let the language set be S={S1,S2,…,Sn}S = \{S_1, S_2, \dots, S_n\}. A homophone triple of string TT means splitting TT into three non-empty consecutive parts T=A+B+CT = A + B + C (where ++ denotes string concatenation), such that each part A,B,CA, B, C is a homophone replacement of some string in SS. Each splitting corresponds to one string triple (A,B,C)(A,B,C), and we call this triple a homophone triple of string TT. Two homophone triples (A1,B1,C1)(A_1, B_1, C_1) and (A2,B2,C2)(A_2, B_2, C_2) are essentially different if and only if A1≠A2A_1 \neq A_2 or B1≠B2B_1 \neq B_2 or C1≠C2C_1 \neq C_2.

Now you are given nn strings S1,S2,…,SnS_1, S_2, \dots, S_n as the language set, and mm strings T1,T2,…,TmT_1, T_2, \dots, T_m as the language materials to be analyzed.
For each TiT_i, please help little ω\omega find how many essentially different homophone triple schemes there are.

Input Format

The first line contains two integers nn and mm (1≤n,m≤1051 \le n, m \le 10^5).

The next nn lines each contain a string SiS_i, representing a word in the language set.

The next mm lines each contain a string TiT_i, representing the material to be analyzed.

Output Format

Output mm lines. The jj-th line (1≤j≤m1 \le j \le m) contains a non-negative integer, indicating how many essentially different homophone triples TjT_j has.

1 1
abbcabb
abbcabbcabb
12

Hint

Sample 1 Explanation

The 1212 essentially different homophone triples are as follows:

  • (a,bbcabb,cabb)(\text{a},\text{bbcabb},\text{cabb})
  • (ab,bcabb,cabb)(\text{ab},\text{bcabb},\text{cabb})
  • (abb,cabb,cabb)(\text{abb},\text{cabb},\text{cabb})
  • (abbc,a,bbcabb)(\text{abbc},\text{a},\text{bbcabb})
  • (abbc,ab,bcabb)(\text{abbc},\text{ab},\text{bcabb})
  • (abbc,abb,cabb)(\text{abbc},\text{abb},\text{cabb})
  • (abbc,abbc,abb)(\text{abbc},\text{abbc},\text{abb})
  • (abbc,abbca,bb)(\text{abbc},\text{abbca},\text{bb})
  • (abbc,abbcab,b)(\text{abbc},\text{abbcab},\text{b})
  • (abbca,b,bcabb)(\text{abbca},\text{b},\text{bcabb})
  • (abbca,bb,cabb)(\text{abbca},\text{bb},\text{cabb})
  • (abbcab,b,cabb)(\text{abbcab},\text{b},\text{cabb})

Constraints

Let ∣X∣|X| be the length of string XX, L1=∑i=1n∣Si∣L_1 = \sum\limits_{i = 1}^{n} |S_i|, and L2=∑i=1m∣Ti∣L_2 = \sum\limits_{i = 1}^{m} |T_i|. For all testdata, it is guaranteed that:

  • 1≤n,m≤1051 \le n , m \le 10^5;
  • 1≤∣Si∣1 \le |S_i|, 3≤∣Ti∣3 \le |T_i|;
  • 1≤L1≤5×1051 \le L_1 \le 5 \times 10^5, 3≤L2≤3×1053 \le L_2 \le 3 \times 10^5;
  • For all 1≤i≤n1 \le i \le n, each SiS_i contains only uppercase and lowercase English letters.
  • For all 1≤i≤m1 \le i \le m, each TiT_i contains only uppercase and lowercase English letters.
Test Point ID n,m≤n , m \le L1L_1 L2≤L_2 \le Special Property
1,21, 2 100100 200200 None
3∼53 \sim 5 10310^3 2 0002\,000 ^
66 ^ 10510^5 A, B
7,87, 8 10410^4 ^ A
9,109, 10 10510^5 B
11,1211, 12 ^ 2×1052 \times 10^5 None
13,1413, 14 5×1055 \times 10^5 3×1053 \times 10^5 A
15,1615, 16 ^ B
17∼2017 \sim 20 None

Special property A: m=1m = 1.

Special property B: For all 1≤i≤n1 \le i \le n, each SiS_i ends with z. For all 1≤i≤m1 \le i \le m, each TiT_i does not contain z.

Translated by ChatGPT 5