#P3735. [HAOI2017] 字符串

[HAOI2017] 字符串

Description

Given a string s s and n n strings pi p_i , for each string pi p_i count the number of its occurrences in s s . Note that the definition of equality between two strings is slightly modified here.

Given a constant k k , for two strings a,b a, b , we define a=b a = b if the following hold:

  1. a=b |a| = |b| .
  2. For all aibi a_i \ne b_i and ajbj a_j \ne b_j , it holds that ij<k |i - j| < k .

If a=bk |a| = |b| \le k , then we regard a=b a = b .

Input Format

The first line contains an integer k k .

The second line contains a string s s .

The third line contains an integer n n , followed by n n lines, each containing a string denoting pi p_i .

All characters have ASCII codes in the range 33 33 to 126 126 .

Output Format

Output n n lines, where the i i -th line is the number of occurrences of pi p_i in s s .

1
xyz
3
xz
y
xzy
2
3
0

Hint

For p1 p_1 , xz=xy,xz=yz xz = xy, xz = yz , because there is only one differing position.

For p2 p_2 , y=x,y=y,y=z y = x, y = y, y = z , similarly.

For p3 p_3 , xzyxyz xzy \ne xyz , the maximum difference =1 = 1 does not satisfy <k=1 < k = 1 .

Constraints

  • For 20% 20\% of the testdata, it holds that: s,Σpi103 |s|, \Sigma |p_i| \le 10^3 .
  • For another 20% 20\% of the testdata, it holds that: n100 n \le 100 .
  • For another 20% 20\% of the testdata, it holds that: s,Σpi5104 |s|, \Sigma |p_i| \le 5 \cdot 10^4 .
  • For 100% 100\% of the testdata, it holds that: s,Σpi2105 |s|, \Sigma |p_i| \le 2 \cdot 10^5 .

Translated by ChatGPT 5