Skip to content

Error in "Compressing a string" section in "Prefix function. Knuth–Morris–Pratt algorithm" #1414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ohwphil opened this issue Jan 19, 2025 · 1 comment

Comments

@ohwphil
Copy link

ohwphil commented Jan 19, 2025

I think "But then the string consists of only one character repeated over and over, hence we can compress it to a string of size  $1$ ..." is false. Shouldn't it be written: "But then the string consists of $gcd(p, k)$ characters repeated over and over, hence we can compress it to a string of size $gcd(p, k)$, and since it divides $p$ it also divides $n$."

Example: k = 4, n = 12, p = 6.

@ohwphil
Copy link
Author

ohwphil commented Jan 19, 2025

I found out that my claim works only when p!=n (Actually the original paragraph also turns out to have this assumption, but it is not explicitly written), so I propose changing the last paragraph wholly.

Now let us assume that  $n$  is not divisible by  $k$ . We show that this implies that the length of the answer is  $n$ . We prove it by contradiction. Assuming there exists an answer, and the compression has length  $p$  ( $p$  divides  $n$, $p \neq n$ ). Then the last value of the prefix function has to be greater than  $n - p$ , i.e. the suffix will partially cover the first block. Now consider the second block of the string. Since the prefix is equal with the suffix, and both the prefix and the suffix cover this block and their displacement relative to each other  $k$  does not divide the block length  $p$  (otherwise  $k$  divides  $n$ ). But then the string consists of $gcd(p, k)$ characters repeated over and over, hence we can compress it to a string of size $gcd(p, k)$, and since this value divides $p$ it also divides $n$. Contradiction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant