Border Theory:There's a fast approach to bruteforce on borders

Правка en16, от OtterZ, 2025-12-21 06:30:10

UPD:Thanks to comment from arvindf232 and pajenegod,I rewrote parts of this post to improve clarity with the help of Chatgpt. I hope it reads more smoothly now.

Written before

In this blog, I want to share Border Theory, a useful and elegant insight about string borders that often appears in competitive programming. Although this idea deserves to be as well-known as the KMP algorithm, many people today aren’t aware of it or how to use it to speed up their solutions.

Main Theoretical Result of Border Theory

Main Result

For a string $$$S$$$, a border is a substring that is both a prefix of $$$S$$$ and a suffix of $$$S$$$.

For a string $$$S$$$, we define that:

$$$ \{b_i\}_{i=1}^n$$$

is the sequence of all lengths of the border of $$$S$$$, sorted in increasing order.

According to the theory,it's garenteed that for all possible $$$S$$$, $$${B_i}_{i = 1}^n$$$ can be partitioned into $$$\operatorname{O}(\log |S|)$$$ continuous subsequences where each subsequence forms an arthmetic progressions.

Proof

First of all,if a string $$$T$$$ is a border of $$$S$$$ and $$$2|T| \ge |S|$$$, Then all positions where $$$T$$$ occurs in $$$S$$$ form an arthmetic progression.

proof

Second, All border lengths satisfying $$$2 \cdot length \ge |S|$$$ also appear in an arithmetic sequence:

proof

Using the two observations, we can show that for every two border length $$$x,y(x \lt y)$$$ do not lie in the same arithmetic subsequence, then $$$2x \lt y$$$, making the theory right.

Practical Use: The Series-Link (shlink) Trick

A practical formulation of this theory is the series link (often written shlink), which groups borders into arithmetic sequences so they can be processed efficiently.

We compute shlink[i] as the start of the next arithmetic progression. (Here link[i] is the usual border/failure link in KMP-style structures, len[i] is the length of the represented substring, and diff[i] is the differance of length between i and link[i].) Now $$$shlink_i$$$ can be calculated like this:

//define shlink[i]
//define link[i] as the maxinum border of i like fail pointers in KMP or PAM
//define len[i] as the length
//define diff[i] as the differance of length between i and link[i]
shlink[i] = link[i];
diff[i] = len[i] - len[link[i]];
if(diff[i] == diff[link[i]])
    shlink[i] = shlink[link[i]];

When we tries to analyze on borders,we found that:

In an arthmetic progression of the border of a prefix of length $$$i$$$ in $$$S$$$,the differance between the arthmetic progression and a similar one at $$$i - diff$$$ are a new border of length $$$i - diff_i$$$ and a new start position $$$i - len_{shlink_i} - diff_i$$$. This allows us to jump across long chains of borders in limited steps.

One example

Link

Given up to $$$5$$$ test cases,each with a string $$$S(1 \le |S| \le 5 \times 10 ^ 5)$$$ and an integer $$$w(1 \le w \le 10 ^ {18})$$$, when we have a string $$$T$$$ we can construct a new string $$$Y$$$ in the two ways: - Append the full string $$$T$$$ and $$$S$$$ and form a string with length $$$|T| + |S|$$$; - If a suffix $$$A$$$ in $$$T$$$ is also a prefix of $$$S$$$, you can overlap them and construct a string of length $$$|T| - |A| + |S|$$$.

Find the number of distinct length of strings in the range $$$[1,w]$$$ that can result from the process.

solution
code

Extention to palindromic suffixes

As we found that The borders of palindormes are also the palindromic suffixes of a palindrome, then we can say that for a string $$$S$$$, the sequence of the lengths of all palindromic suffixes in increasing order can be partitioned into $$$\operatorname{O}(\log |S|)$$$ continuous arthmetic progressions. We can also say that the sequence of the lengths of all even-length palindromic suffixes in increasing order can be partitioned into $$$\operatorname{O}(\log |S|)$$$ continuous arthmetic progressions.

Tasks

In 932G - Palindrome Partition we can make a string $$$T$$$ by:

  • $$$t_{2k - 1} = s_{k}$$$
  • $$$t_{2k} = s_{n - k + 1}$$$

Then we found that The task turns to the number of ways to divide $$$T$$$ into several even-length palindromes, we tries to solve it using dynamic planning as $$$dp_i$$$ is the answer when the string is the prefix of length $$$i$$$ in the string $$$T$$$, then we use shlink pointers to speed it up.

code

Such solution is similar to 906E - Reverses,which turns:

  • $$$t_{2k - 1} = x_k$$$
  • $$$t_{2k} = y_k$$$

And use dynamic planning to get the divition to even-length palindromes with least palindromes not with the length of $$$2$$$.

code

Summary

Though bruteforce on borders is slow, we can approach using border theory,that shows that border lengths cluster into structured arithmetic sequences bounded in number by $$$\operatorname{O}(\log |S|)$$$ to make it much faster.

Теги #strings, border, #brute force

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en25 Английский OtterZ 2025-12-22 11:32:10 84
en24 Английский OtterZ 2025-12-22 05:32:17 157
en23 Английский OtterZ 2025-12-22 04:20:06 992 Tiny change: 'so $p = q$\n</spoile' -> 'so $p = q$.\n</spoile'
en22 Английский OtterZ 2025-12-21 11:03:05 35
en21 Английский OtterZ 2025-12-21 10:22:19 6
en20 Английский OtterZ 2025-12-21 10:21:10 200
en19 Английский OtterZ 2025-12-21 10:17:25 27
en18 Английский OtterZ 2025-12-21 08:05:21 171
en17 Английский OtterZ 2025-12-21 07:59:36 13 Tiny change: 'now.\n\n# Written before\n\nIn th' -> 'now.\n\n# Preface\n\nIn th'
en16 Английский OtterZ 2025-12-21 06:30:10 25 Tiny change: 'th $i + q$.\n\nAs a' -> 'th $i + q$,one of the border of $S$.\n\nAs a'
en15 Английский OtterZ 2025-12-21 05:45:52 1413
en14 Английский OtterZ 2025-12-20 06:19:26 2
en13 Английский OtterZ 2025-12-20 04:57:35 4468 Tiny change: ' that for every possible $S$,${B_i}_{i ' -> ' that for all possible $S$, ${B_i}_{i '
en12 Английский OtterZ 2025-12-19 09:05:15 265
en11 Английский OtterZ 2025-12-19 08:45:48 475
en10 Английский OtterZ 2025-12-19 08:29:45 6 Tiny change: 'eed modules.\n\nBut t' -> 'eed module result.\n\nBut t'
en9 Английский OtterZ 2025-12-19 07:27:38 1 Tiny change: 'p,q \text{are the le' -> 'p,q \text{ are the le'
en8 Английский OtterZ 2025-12-19 07:26:49 1 Tiny change: ' palindroms, we trie' -> ' palindromes, we trie'
en7 Английский OtterZ 2025-12-19 05:19:03 1 Tiny change: 'n\n## Tasks\n\n[Link]' -> 'n\n## Task\n\n[Link]'
en6 Английский OtterZ 2025-12-19 04:40:02 4594 (published)
en5 Английский OtterZ 2025-12-19 04:20:15 65
en4 Английский OtterZ 2025-12-19 04:19:26 770 Tiny change: 'ce,can be got like this' -> 'ce,can be calculated like this'
en3 Английский OtterZ 2025-12-19 03:29:52 4119
en2 Английский OtterZ 2025-12-18 16:53:34 6
en1 Английский OtterZ 2025-12-18 16:48:23 2086 Initial revision (saved to drafts)