Here we discuss two problems at once. Given a string s of length n . In the first variation of the problem we want to count the number of appearances of each prefix s[0…i] in the same string. In the second variation of the problem another string t is given and we want to count the number of appearances of each prefix in
Assistant
s
[
0
…
i
]
t .
First we solve the first problem. Consider the value of the prefix function
π[i] at a position i . By definition it means that the prefix of length π[i] of the string s occurs and ends at position i , and there is no longer prefix that follows this definition. At the same time shorter prefixes can end at this position. It is not difficult to see, that we have the same question that we already answered when we computed the prefix function itself: Given a prefix of length j that is a suffix ending at position i , what is the next smaller prefix <j that is also a suffix ending at position i . Thus at the position i ends the prefix of length π[i] , the prefix of length π[π[i]−1] , the prefix
π[π[π[i]−1]−1] , and so on, until the index becomes zero. Thus we can compute the answer in the following way.
vector ans(n + 1);
for (int i = 0; i < n; i++)
ans[pi[i]]++;
for (int i = n-1; i > 0; i--)
ans[pi[i-1]] += ans[i];
for (int i = 0; i <= n; i++)
ans[i]++;
Here for each value of the prefix function we first count how many times it occurs in the array
π , and then compute the final answers: if we know that the length prefix i appears exactly ans[i] times, then this number must be added to the number of occurrences of its longest suffix that is also a prefix. At the end we need to add 1 to each result, since we also need to count the original prefixes also. i can't understand this please explain me with example
Certainly! Let's work through an example step-by-step to understand each part of the algorithm.
Let's take the string s = "abababca", which has a length of n = 8.
First, we'll compute the prefix function π for this string. The prefix function π[i] for position i of the string is defined as the length of the longest proper prefix of the substring s[0…i] which is also a suffix of this substring.
Step 1: Compute the Prefix Function π
Here's how to compute the prefix function for each position in the string:
Initialize:
Set π[0] = 0 (since the first character has no proper prefix).
Iteratively compute π[i] for i from 1 to n - 1:
At each position i, we'll find the longest proper prefix which is also a suffix ending at position i.
Let's fill out the values:
i = 1: s[1] = 'b'
k = π[0] = 0
Since s[k] ('a') ≠ s[1] ('b'), and k = 0, we set π[1] = 0.
i = 2: s[2] = 'a'
k = π[1] = 0
Since s[k] ('a') = s[2] ('a'), we increment k to 1.
Set π[2] = k = 1.
i = 3: s[3] = 'b'
k = π[2] = 1
Since s[k] ('b') = s[3] ('b'), increment k to 2.
Set π[3] = k = 2.
i = 4: s[4] = 'a'
k = π[3] = 2
Since s[k] ('a') = s[4] ('a'), increment k to 3.
Set π[4] = k = 3.
i = 5: s[5] = 'b'
k = π[4] = 3
Since s[k] ('b') = s[5] ('b'), increment k to 4.
Set π[5] = k = 4.
i = 6: s[6] = 'c'
k = π[5] = 4
s[k] ('a') ≠ s[6] ('c'), fallback to k = π[k-1] = π[3] = 2.
s[k] ('a') ≠ s[6] ('c'), fallback to k = π[k-1] = π[1] = 0.
s[k] ('a') ≠ s[6] ('c'), since k = 0, set π[6] = 0.
i = 7: s[7] = 'a'
k = π[6] = 0
Since s[k] ('a') = s[7] ('a'), increment k to 1.
Set π[7] = k = 1.
So the prefix function π for the string s is:
π = [0, 0, 1, 2, 3, 4, 0, 1]
Step 2: Initialize ans Array
We will use an array ans to keep track of the number of occurrences of each prefix length. We initialize the array ans with n + 1 elements, all set to zero:
In the first loop, we count how many times each length of prefix appears as a border in the prefix function π:
cpp
for (int i = 0; i < n; i++)
ans[π[i]]++;
Let's fill in the values step by step:
For i = 0: π[0] = 0 ⇒ ans[0] += 1 ⇒ ans[0] = 1
For i = 1: π[1] = 0 ⇒ ans[0] += 1 ⇒ ans[0] = 2
For i = 2: π[2] = 1 ⇒ ans[1] += 1 ⇒ ans[1] = 1
For i = 3: π[3] = 2 ⇒ ans[2] += 1 ⇒ ans[2] = 1
For i = 4: π[4] = 3 ⇒ ans[3] += 1 ⇒ ans[3] = 1
For i = 5: π[5] = 4 ⇒ ans[4] += 1 ⇒ ans[4] = 1
For i = 6: π[6] = 0 ⇒ ans[0] += 1 ⇒ ans[0] = 3
For i = 7: π[7] = 1 ⇒ ans[1] += 1 ⇒ ans[1] = 2
After this loop, ans looks like this:
ans = [3, 2, 1, 1, 1, 0, 0, 0, 0]
Step 4: Second Loop - Propagate Counts to Borders
In the second loop, we propagate the counts of longer prefixes to their borders because every occurrence of a longer prefix implies an occurrence of its borders:
cpp
for (int i = n - 1; i > 0; i--)
ans[π[i - 1]] += ans[i];
We process the array from the end to the start:
For i = 7:
π[6] = π[6] = 0
ans[0] += ans[7] ⇒ ans[0] += 0 ⇒ ans[0] remains 3
For i = 6:
π[5] = π[5] = 4
ans[4] += ans[6] ⇒ ans[4] += 0 ⇒ ans[4] remains 1
For i = 5:
π[4] = π[4] = 3
ans[3] += ans[5] ⇒ ans[3] += 0 ⇒ ans[3] remains 1
For i = 4:
π[3] = π[3] = 2
ans[2] += ans[4] ⇒ ans[2] += 1 ⇒ ans[2] = 2
For i = 3:
π[2] = π[2] = 1
ans[1] += ans[3] ⇒ ans[1] += 1 ⇒ ans[1] = 3
For i = 2:
π[1] = π[1] = 0
ans[0] += ans[2] ⇒ ans[0] += 2 ⇒ ans[0] = 5
For i = 1:
π[0] = π[0] = 0
ans[0] += ans[1] ⇒ ans[0] += 3 ⇒ ans[0] = 8
After this loop, ans looks like this:
ans = [8, 3, 2, 1, 1, 0, 0, 0, 0]
Step 5: Third Loop - Add the Original Prefix Occurrences
Every prefix naturally occurs at least once (the prefix itself at the start of the string), so we need to account for those by adding 1 to each count:
cpp
for (int i = 0; i <= n; i++)
ans[i]++;
This gives us the final counts:
ans = [9, 4, 3, 2, 2, 1, 1, 1, 1]
Step 6: Interpret the ans Array
Now, ans[i] represents the number of times the prefix of length i occurs in the string s. Let's list them:
Prefix length 1 ('a') occurs 4 times.
Prefix length 2 ('ab') occurs 3 times.
Prefix length 3 ('aba') occurs 2 times.
Prefix length 4 ('abab') occurs 2 times.
Prefix length 5 ('ababa') occurs 1 time.
Prefix length 6 ('ababab') occurs 1 time.
Prefix length 7 ('abababc') occurs 1 time.
Prefix length 8 ('abababca') occurs 1 time.
Verification
Let's verify these counts by finding all occurrences of each prefix in s:
'a' occurs at positions: 0, 2, 4, 7 ⇒ 4 times.
'ab' occurs at positions: 0, 2, 4 ⇒ 3 times.
'aba' occurs at positions: 0, 2 ⇒ 2 times.
'abab' occurs at positions: 0, 2 ⇒ 2 times.
'ababa' occurs at position: 0 ⇒ 1 time.
'ababab' occurs at position: 0 ⇒ 1 time.
'abababc' occurs at position: 0 ⇒ 1 time.
'abababca' occurs at position: 0 ⇒ 1 time.
Understanding Why the Algorithm Works
First Loop Explanation:
We count the number of times each possible border length (from π[i]) appears in the entire string. This gives us a count of how many times prefixes of different lengths appear as borders (patterns that are both proper prefixes and suffixes ending at position i).
Second Loop Explanation:
We propagate counts from longer prefixes to their borders. This is because every occurrence of a longer prefix implies an occurrence of its borders (since a border is a prefix of a prefix). By adding the counts from longer prefixes to their respective borders (using the prefix function), we accumulate the total number of occurrences for each prefix length.
Third Loop Explanation:
We add 1 to each count to account for the prefix itself occurring at the start of the string.
Conclusion
By following these steps and understanding how each loop contributes to the final counts, we can see how the algorithm efficiently calculates the number of occurrences of each prefix within the string.
This method leverages the properties of the prefix function and the relationships between prefixes and their borders to avoid redundant computations, leading to an efficient solution.
I hope this example clarifies how the algorithm works and how it calculates the number of occurrences of each prefix in the string!