Agitations
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Before the elections, there is always a period of agitation. Different parties hold rallies in city squares, constantly chanting slogans. One election day, Zenyk decided to take a walk around the city. While walking along the central streets of his hometown, he heard various slogans echoing from everywhere. However, he only heard the endings of the phrases, meaning that if they chanted "Слава Україні!", Zenyk might have heard "...ні" or "...країні".
Unfortunately, the agitators are not very creative people, and they constantly chant the same slogan. However, they are very persistent and chant this slogan many times in a row. Zenyk became curious about how many times they actually chanted this slogan.
You are given the slogan \(p\) that the agitators chanted. You are also given the string \(r\), which is the concatenation of the endings of the string \(p\) — everything that Zenyk heard. You need to determine the minimum number of times the slogan \(p\) had to be repeated so that Zenyk heard what he heard.
Input
The first line contains the string \(p\) — the slogan chanted by the agitators.
The second line contains the string \(r\) — the concatenation of everything Zenyk heard.
Each string consists exclusively of lowercase Latin letters.
Output
In a single line, print a single integer — the minimum number of endings of the string \(p\) required to form the string \(r\).
It is guaranteed that the result always exists, i.e., the string \(r\) can always be formed from the endings of the string \(p\).
Constraints
\(1 \le |p|, |r| \le 10^5\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| glorytoukraine ukraineaineaine | 3 |
Notes
Zenyk heard ukraine, aine,
aine.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Agitations - Editorial
Reverse the strings \(p\) and \(r\), and call the results \(p'\) and \(r'\).
Now the task is to find the smallest number of prefixes of \(p'\) whose concatenation gives \(r'\).
For convenience, let \(n\) and \(m\) be the lengths of \(p'\) and \(r'\).
Let’s prove that there is an optimal answer in which the last prefix is as long as possible. For this, consider two different suffixes \(r'[i..m - 1]\) and \(r'[j..m - 1]\), \(i < j\), both of which are prefixes of \(p'\). Let an optimal answer contain the prefix \(r'[j..m - 1]\). Replace the last prefix in this answer with \(r'[i..m - 1]\); the number of elements of the answer does not change. Now there is a segment \(r'[i..j - 1]\) each character of which is covered by two prefixes of \(p'\). All old prefixes entirely within the segment \([i..j - 1]\) can now be dropped, which can only decrease the number of elements of the answer. And the only prefix \(r'[l..k]\) with \(l < i \le k\), that is, partly covered by the prefix \(r'[i..m - 1]\), can be replaced with the prefix \(r'[l..i - 1]\), because if \(r'[l..k]\) is a prefix of \(p'\), then \(r'[l..i - 1]\) is also a prefix of \(p'\). After this replacement, every element of \(r'\) is covered by exactly one prefix, and the number of prefixes cannot increase. So this process turns any answer, including an optimal one, into one where the last prefix has the maximum possible length. Having fixed the last prefix, we can start extending the second to last one by the same reasoning, and so on.
As a result, one of the optimal answers can be built with the following greedy algorithm: find the longest prefix of \(p'\) that matches a suffix of \(r'\), remove this suffix and increase the answer by 1. Repeat while \(r'\) is not empty.
To quickly find the longest suffix of \(r'\) that matches a prefix of \(p'\), we can use the prefix function. Namely, compute the prefix function \(\pi\) of the string \(p' + \$ + r'\). Then the length of the longest prefix of \(p'\) that matches a suffix of \(r'\) is the value of \(\pi\) at position \(n + m\). The optimal lengths of all the following prefixes can be found in the same array by subtracting the length of the last prefix from the position each time.
The complexity of the algorithm is \(O(n + m)\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|