[Tutorial] Dictionary of Basic Factors — O(1) String Matching

Правка en3, от Leonardo16, 2021-06-23 00:33:42

Hello, codeforces.
I think this technique is important, and easy to code, although I haven't found much about it, so I've decided to make a blog explaining it.This technique allows to compare substrings lexicographically in O (1) with a preprocessing in O (NlogN).
As this is my first post on codeforces, please let me know if there are any mistakes.

Prerequisites:
  • Basic strings knowledge.
  • Sparse Table(Optional).

What Dictionary of Basic Factors is?

As in the sparse table we will have a matrix of size

Unable to parse markup [type=CF_MATHJAX]

called $$$DFB$$$, where $$$DFB[i][j]$$$ tells us the position of the string $$$S[i ... i + 2^j-1]$$$ (the first time it appears) among all the strings of size $$$2^j$$$ ordered lexicographically.

So, how do we build the matrix? Let's start with the lowest level, for powers of size $$$2^0$$$, we simply put the position of the character $$$S[i]$$$ in the dictionary (1 for A, 2 for B ... and so on).

For the following $$$ DFB[i][j] $$$ we can save a pair of numbers that are

Unable to parse markup [type=CF_MATHJAX]



Now we replace the pairs by their position (the first time it appears) in the sorted array of pairs in that power of two
We do that for all powers of two and we will have the complete matrix.
Handling queries:

Proof:

Теги #strings, #string matching

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en13 Английский Leonardo16 2021-06-29 00:24:36 22
en12 Английский Leonardo16 2021-06-28 21:05:23 111
en11 Английский Leonardo16 2021-06-24 20:08:52 237 Tiny change: 'strings. \nThis t' -> 'strings. \nThis t'
en10 Английский Leonardo16 2021-06-23 02:53:32 4 Tiny change: '[2], DBF [2] [2])$, w' -> '[2], DBF [3] [2])$, w' (published)
en9 Английский Leonardo16 2021-06-23 02:39:03 663 Tiny change: ' in $O(N)$ (I'll explain it more deeply later), otherwis' -> ' in $O(N)$, otherwis'
en8 Английский Leonardo16 2021-06-23 02:23:48 4 Tiny change: 'hose pairs, this can be' -> 'hose pairs. This can be'
en7 Английский Leonardo16 2021-06-23 02:22:01 8 Tiny change: 'irst time it appears) ' -> 'irst time the pair appears) '
en6 Английский Leonardo16 2021-06-23 02:20:42 44
en5 Английский Leonardo16 2021-06-23 01:41:15 810 Tiny change: 'he string ABACABA and we wa' -> 'he string "ABACABA" and we wa'
en4 Английский Leonardo16 2021-06-23 01:03:39 602 Tiny change: 'l sort in $N\log{N}$(But this ' -> 'l sort in O(NlogN) (But this '
en3 Английский Leonardo16 2021-06-23 00:33:42 73 Tiny change: 'ize $N\logN$ called ' -> 'ize $N\log N$ called '
en2 Английский Leonardo16 2021-06-23 00:30:48 153
en1 Английский Leonardo16 2021-06-23 00:27:13 1289 Initial revision (saved to drafts)