2
$\begingroup$

Let $s$ be a string of length $n$. One of the classical solutions to the problem of finding the smallest period $p$ of $s$ (that is, smallest $p$ such that $s$ can be obtained as a concatenation of the several copies of $p$) uses so-called $Z$-function. In words, $Z=Z[0..n]$ with $Z[0] = 0$ and $Z[i]$ is the length of the longest substring starting at position $i$ which coincides with some prefix of $s$. Given that, it is easy to solve the problem of the smallest period: we iterate through $i$, and if at some point we find $i$ such that $i + z[i] = n$ and $n$ modulo $i$ is 0ドル$, then $s[0..i-1]$ is our answer.

Now back to the question. Suppose $s$, as before, is a prefix (of length no more than, say, 10ドル^6$) of an infinite string $ppppppppp...$ obtained by concatenation of infinitely many copies of $p$. The goal is to recover the smallest possible $p$. Can we adopt the algorithm above to solve the problem?

Example: $s = abcabca$, the answer is $p=abc$.

asked Jun 17, 2021 at 19:53
$\endgroup$

1 Answer 1

2
$\begingroup$

If at some point we find $i$ such that $i + z[i] \ge n$, then the smallest possible $p$ is $s[0..i-1]$.

answered Jun 19, 2021 at 16:27
$\endgroup$
1
  • $\begingroup$ The proof for the correctness of this approach is almost the same for the case of finding the smallest pure period of $s$. It looks like there is no question on the latter case. $\endgroup$ Commented Jun 20, 2021 at 4:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.