File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -24,10 +24,17 @@ \subsection{Z Function}
24
24
\begin {flushleft }
25
25
$ z[i] = $ length of the longest substring starting at i that is a prefix of s \\
26
26
$ z[i] = $ max len s.t. $ s[0 ..len-1 ] == s[i..i+len-1 ]$ , $ z[0 ] = 0 $
27
-
28
27
\end {flushleft }
29
28
\snippet {source/z_func.h}
30
29
30
+ \subsection {Periods }
31
+ \bigo {N}
32
+
33
+ $ k \leq N$ is a period of a string $ s$ iff $ \forall i, s[i] = s[i\% k]$ . \\
34
+ Note this means that 2 is a valid period of "aba"
35
+ (uncomment the line if this is not wanted)
36
+ \snippet {source/periods.h}
37
+
31
38
\subsection {KMP }
32
39
\bigo {N} construction, \bigo {N+M} search with \bigo {|pat|} space.
33
40
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+ #include "common.h"
3
+ #include "z_func.h"
4
+
5
+ vi periods (string s ) {
6
+ vi z = z_function (s ), p ;
7
+ int n = ssize (z );
8
+ for (int i = 1 ; i < n ; i ++ ) {
9
+ // if(n%i == 0) // uncomment for no trailing subperiods
10
+ if (z [i ]+ i == n )
11
+ p .push_back (i );
12
+ }
13
+ p .push_back (n );
14
+ return p ;
15
+ }
You can’t perform that action at this time.
0 commit comments