One mismatch

0

0 votes
Approved, Arrays, Data Structures, Hard
Problem

Given a string s and a set of n strings pi.

For each i find the number of occurrences of pi in s with at most one k-length gap.

The string s occurs in string t at position p with at most one k-length gap if strings s and tptp+1tp+|s|1 are equal with at most one k-length gap.

Strings s and r are equal with at most one k-length gap if:

  1. |s|=|r|;
  2. or all i and j such that siri and sjrj, it holds |ij|<k.

Input format

First line of input contains single integer k (1k2105).

Second line of input contains string s (1|s|2105).

Third line contains an integer n (1n2105).

Then follow n lines. The i-th of them contains string pi (ni=1|pi|2105).

s and pi can contain characters with codes from 33 to 126, inclusive.

Output format

Print n lines. The i-th of them should contain the number of occurrences pi in s with at most one k-length gap.

Sample Input
1
abc
3
ac
b
acb
Sample Output
2
3
0
Time Limit: 1
Memory Limit: 512
Source Limit:
Explanation
  • "ac" equals "ab" and "bc"
  • "b" equals "a", "b" and "c"
  • "acb" doesn't equal "abc"
Editor Image

?