CF BUDDY
← Problems·

1721E · Prefix Function Queries

2200 · dfs and similar, dp, hashing

Problem: You are given a string ss, consisting of lowercase Latin letters.

You are asked qq queries about it: given another string tt, consisting of lowercase Latin letters, perform the following steps:

  1. concatenate ss and tt;
  2. calculate the prefix function of the resulting string s+ts+t;
  3. print the values of the prefix function on positions s+1,s+2,,s+t|s|+1, |s|+2, \dots, |s|+|t| (s|s| and t|t| denote the lengths of strings ss and tt, respectively);
  4. revert the string back to ss.

The prefix function of a string aa is a sequence p1,p2,,pap_1, p_2, \dots, p_{|a|}, where pip_i is the maximum value of kk such that k<ik < i and a[1..k]=a[ik+1..i]a[1..k]=a[i-k+1..i] (a[l..r]a[l..r] denotes a contiguous substring of a string aa from a position ll to a position rr, inclusive). In other words, it's the longest proper prefix of the string a[1..i]a[1..i] that is equal to its suffix of the same length.

Input Format: The first line contains a non-empty string ss (1s1061 \le |s| \le 10^6), consisting of lowercase Latin letters.

The second line contains a single integer qq (1q1051 \le q \le 10^5) — the number of queries.

Each of the next qq lines contains a query: a non-empty string tt (1t101 \le |t| \le 10), consisting of lowercase Latin letters.

Output Format: For each query, print the values of the prefix function of a string s+ts+t on positions s+1,s+2,,s+t|s|+1, |s|+2, \dots, |s|+|t|.

Sample Cases

Case 1

Input

aba
6
caba
aba
bababa
aaaa
b
forces

Output

0 1 2 3 
1 2 3 
2 3 4 5 6 7 
1 1 1 1 
2 
0 0 0 0 0 0

Case 2

Input

aacba
4
aaca
cbbb
aab
ccaca

Output

2 2 3 1 
0 0 0 0 
2 2 0 
0 0 1 0 1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback