Addition: Palindromic Tree#205
Conversation
simonlindholm
left a comment
There was a problem hiding this comment.
Thanks, this seems good to have. (Closes issue #22.)
| PalinTree(const string &s) : str(s) { | ||
| fail = len = pos = lz = freq = vi(sz(s) + 2); | ||
| nxt.resize(sz(s) + 2); | ||
| n = cur = fail[0] = fail[1] = 0; |
There was a problem hiding this comment.
fail[0] and fail[1] are automatically 0, and n and cur can have = 0 at their declarations
| * Status: stress-tested | ||
| */ | ||
| #pragma once | ||
|
|
There was a problem hiding this comment.
Indent with tabs (which will expand to 2 spaces in the pdf)
| nxt.resize(sz(s) + 2); | ||
| n = cur = fail[0] = fail[1] = 0; | ||
| addNode(-1, -1), addNode(0, 0); | ||
| rep(i, 0, sz(s)) addChar(i, s[i]); |
There was a problem hiding this comment.
| rep(i, 0, sz(s)) addChar(i, s[i]); | |
| rep(i,0,sz(s)) addChar(i, s[i]); |
(kactl doesn't use spaces in rep macro arguments except when they are long)
| for (int i = n - 1; ~i; --i) { | ||
| freq[i] += lz[i]; | ||
| lz[fail[i]] += lz[i]; | ||
| lz[i] = 0; |
There was a problem hiding this comment.
this is set up for propagate being called multiple times, but I don't see the point of that since it's O(n) which matches the full tree construction?
would it make sense to replace freq by lz, and remove this resetting?
| int u = getFailure(cur, i); | ||
| int &ch = nxt[u][c]; |
There was a problem hiding this comment.
| int u = getFailure(cur, i); | |
| int &ch = nxt[u][c]; | |
| int u = getFailure(cur, i), &ch = nxt[u][c]; |
| } | ||
|
|
||
| int getFailure(int u, int i) { | ||
| while (i <= len[u] || str[i] != str[i - len[u] - 1]) u = fail[u]; |
There was a problem hiding this comment.
this line goes past 63 chars, wrap it before u = or golf variable names
| rep(i, 0, sz(s)) addChar(i, s[i]); | ||
| propagate(); | ||
| } | ||
|
|
There was a problem hiding this comment.
let's squeeze this a bit by removing blank lines between methods
| int &ch = nxt[u][c]; | ||
| if (~ch) return (void) ++lz[cur = ch]; | ||
| int v = cur = ch = addNode(len[u] + 2, i - len[u] - 1); | ||
| fail[v] = len[v] == 1 ? 1 : nxt[getFailure(fail[u], i)][c]; |
There was a problem hiding this comment.
this can use ch or cur instead of v (assuming getFailure is shorted to avoid line-wrapping)
| \kactlimport{SuffixTree.h} | ||
| \kactlimport{Hashing.h} | ||
| \kactlimport{AhoCorasick.h} | ||
| \kactlimport{PalindromicTree.h} |
There was a problem hiding this comment.
(it's a bit sad to have both PalindromicTree and Manacher, but I guess they kinda compute different things)
|
|
||
| int addNode(int l, int p) { | ||
| nxt[n].assign(A, -1); | ||
| len[n] = l, pos[n] = p, lz[n] = 1, freq[n] = 0; |
There was a problem hiding this comment.
| len[n] = l, pos[n] = p, lz[n] = 1, freq[n] = 0; | |
| len[n] = l, pos[n] = p, lz[n] = 1; |
|
May be worth looking at https://github.com/SuprDewd/CompetitiveProgramming/blob/master/code/strings/eertree.cpp for code golf inspiration |
This is my implementation of palindromic tree; a strings data structure that extracts all unique palindromic substrings and their frequencies. Obviously I'm open to suggestions on how to improve it : D