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

Revision en14, by OtterZ, 2025-12-20 06:19:26

UPD:Thanks to comment from arvindf232 and pajenegod, the blog has been developed with the help of Chatgpt. Hope that such a development will make this blog more readable.

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

Conclution

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 there's a border $$$T$$$ of $$$S$$$ that $$$2|T| \ge |S|$$$, Then all the positions where $$$T$$$ occur in $$$S$$$ follow an arthmetic pattern.

proof

Second, All border lengths satisfying that inequality also appear in an arithmetic sequence:

proof

Using the two lemma we can proof that for every two border length $$$x,y(x \lt y)$$$ not in the same subsequence, $$$2x \lt y$$$ is garenteed, making the theory right.

Practicle way to use border theory:Shlink pointer

As $$$shlink_i$$$ means to a node $$$i$$$,which node is the start of the next arthmetic sequence,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 to arthmetic progression and a similar one at $$$i - diff$$$ are a new border of length $$$i - diff_i$$$ and a new position that a border occurs as suffix $$$i - len_{shlink_i} - diff_i$$$. Which is really convenient to make use of Border Theory.

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.

Tags #strings, border, #brute force

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en25 English OtterZ 2025-12-22 11:32:10 84
en24 English OtterZ 2025-12-22 05:32:17 157
en23 English OtterZ 2025-12-22 04:20:06 992 Tiny change: 'so $p = q$\n</spoile' -> 'so $p = q$.\n</spoile'
en22 English OtterZ 2025-12-21 11:03:05 35
en21 English OtterZ 2025-12-21 10:22:19 6
en20 English OtterZ 2025-12-21 10:21:10 200
en19 English OtterZ 2025-12-21 10:17:25 27
en18 English OtterZ 2025-12-21 08:05:21 171
en17 English 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 English 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 English OtterZ 2025-12-21 05:45:52 1413
en14 English OtterZ 2025-12-20 06:19:26 2
en13 English 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 English OtterZ 2025-12-19 09:05:15 265
en11 English OtterZ 2025-12-19 08:45:48 475
en10 English OtterZ 2025-12-19 08:29:45 6 Tiny change: 'eed modules.\n\nBut t' -> 'eed module result.\n\nBut t'
en9 English OtterZ 2025-12-19 07:27:38 1 Tiny change: 'p,q \text{are the le' -> 'p,q \text{ are the le'
en8 English OtterZ 2025-12-19 07:26:49 1 Tiny change: ' palindroms, we trie' -> ' palindromes, we trie'
en7 English OtterZ 2025-12-19 05:19:03 1 Tiny change: 'n\n## Tasks\n\n[Link]' -> 'n\n## Task\n\n[Link]'
en6 English OtterZ 2025-12-19 04:40:02 4594 (published)
en5 English OtterZ 2025-12-19 04:20:15 65
en4 English OtterZ 2025-12-19 04:19:26 770 Tiny change: 'ce,can be got like this' -> 'ce,can be calculated like this'
en3 English OtterZ 2025-12-19 03:29:52 4119
en2 English OtterZ 2025-12-18 16:53:34 6
en1 English OtterZ 2025-12-18 16:48:23 2086 Initial revision (saved to drafts)