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

Revision en3, by OtterZ, 2025-12-19 03:29:52

Written before

Border Theory,which was not well-known enough,should have been publicized together with KMP or similar algorithms.But nowadays lots of people don't know such a theory exist,making them unable to solve several tasks easily or even can't get a fast solution.So it's time to complete the blog as what I will do on this.

Ordinary Border Theory

Conclution

For a string $$$S$$$, There are a squence of strings:

$$$b_i(0 = b_1 \lt b_2 \lt b_3 \lt ... \lt b_n = |S|,S_{1,...,b_i} = S_{n - b_i + 1,...,n})$$$

it's garenteed that there exist a way to divide the sequence into $$$\operatorname{O}(\log |S|)$$$ continuous subsequence that each subsequence forms an arithmetic sequence.

Proof

First of all,we found that if there's a string $$$T$$$ as a border of $$$S$$$ and $$$2|T| \ge |S|$$$, Then the sequence of where $$$T$$$ appears in $$$S$$$ forms an arthmetic sequence.

proof

Second,We found that all lengths of border $$$T$$$ that $$$2|T| \ge |S|$$$ will form an arthmetic sequence:

proof

So there's a way that if $$$p \lt q,p,q \text{are the length of borders}$$$ not in the same continuous sequence, $$$2p \lt q$$$ exist,So the conclution is right.

Tasks

Task $$$1$$$:given at most $$$5$$$ cases in a test case,each cases gives 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 make the new string by $$$|T| + |S|$$$ or if a suffix $$$A$$$ in $$$T$$$ is also the prefix of $$$S$$$, $$$T - A + S$$$ can be formed as there's a string of length $$$|T| - |A| + |S|$$$.Find the number of distinct length of strings in $$$[1,w]$$$ can be formed.

solution
code
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)