You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/string/suffix-automaton.md
+12-12Lines changed: 12 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -502,9 +502,9 @@ This is demonstrated succinctly below:
502
502
503
503
```cpp
504
504
long long get_diff_strings(){
505
-
lltot{};
506
-
for(int i=1;i<sz;i++) {
507
-
tot+=st[i].len-st[st[i].link].len;
505
+
long longtot = 0;
506
+
for(int i = 1; i <sz;i++) {
507
+
tot +=st[i].len -st[st[i].link].len;
508
508
}
509
509
return tot;
510
510
}
@@ -530,24 +530,24 @@ We take the answer of each adjacent vertex $w$, and add to it $d[w]$ (since ever
530
530
Again this task can be computed in $O(length(S))$ time.
531
531
532
532
Alternatively, we can, again, take advantage of the fact that each state $v$ matches to substrings of length $[minlen(v),len(v)]$.
533
-
Since $minlen(v) = 1 + len(link(v))$ and the arithmetic series formula $S_n = n* (a_1+a_n) / 2$ (where $S_n$ denotes the sum of $n$ terms, $a_1$ representing the first term, and $a_n$ representing the last), we can compute the length of substrings at a state in constant time. We then sum up these totals for each state $v \neq t_0$ in the automaton. This is shown by the code below:
533
+
Since $minlen(v) = 1 + len(link(v))$ and the arithmetic series formula $S_n = n\cdot \frac{a_1+a_n}{2}$ (where $S_n$ denotes the sum of $n$ terms, $a_1$ representing the first term, and $a_n$ representing the last), we can compute the length of substrings at a state in constant time. We then sum up these totals for each state $v \neq t_0$ in the automaton. This is shown by the code below:
534
534
535
535
```cpp
536
536
longlongget_tot_len_diff_substings() {
537
-
long long tot{};
538
-
for(int i=1;i<sz;i++) {
539
-
long long shortest=st[st[i].link].len+1;
540
-
long long longest=st[i].len;
537
+
long long tot = 0;
538
+
for(int i = 1; i <sz;i++) {
539
+
long long shortest =st[st[i].link].len +1;
540
+
long long longest =st[i].len;
541
541
542
-
long long num_strings=longest-shortest+1;
543
-
544
-
long long cur=num_strings*(longest+shortest)/2;
542
+
long long num_strings = longest - shortest + 1;
543
+
long long cur = num_strings * (longest + shortest) / 2;
545
544
tot += cur;
546
545
}
547
546
return tot;
548
547
}
549
548
```
550
-
This approaches runs in $O(length(S))$ time, but experimentally runs 20x faster than the memoized dynamic programming version on randomized strings. It requires no extra space and not recursion.
549
+
550
+
This approaches runs in $O(length(S))$ time, but experimentally runs 20x faster than the memoized dynamic programming version on randomized strings. It requires no extra space and no recursion.