Hang Technology Multi-school HDU 6599 I Love Palindrome String (Back Tree) Question

Title meaning:

Define a string as \(super\) palindrome string as:< br> \(\bullet\) The string s is a substring of the main string str, namely \(s = str_lstr_{l + 1} \cdots str_r\)
\(\bullet\) string s is a palindrome string
\(\bullet\) string \(str_lstr_{l + 1}…str_{\llcorner (l + r) / 2 \lrcorner}\) is also a palindrome string
\(\cdots n\) of \(super \) The palindrome appeared several times.

Idea:

Build a palindrome tree, and then use it every time you create a new node Hash quickly judge whether it is a \(super\) palindrome string, and then count the palindrome tree.

Code:

#include#include#include#include#include#include#include#include#include#include#include#include#includeusing namespace std;typedef long long ll; typedef unsigned long long ull;const int maxn = 3e5 + 5;const int INF = 0x3f3f3f3f;const ull seed = 131;const ll MOD = 1e9 + 7;using namespace std;ull ha[maxn], fac[maxn];int ans[maxn];ull getstring(int l, int r){ return ha[r]-ha[l-1] * fac[r-l + 1];}struct PAM{ int nex[maxn][26]; //Point to a character node int fail[maxn]; //Mismatch node int len[maxn]; //The length of the palindrome of the current node int str[maxn]; //Currently added string int cnt[maxn] ; //Number of node occurrences int last; int tot; //Number of nodes in PAM int N; //Number of strings added int satisfy[maxn]; int newnode(int L){ for(int i = 0; i <26; i++) nex[tot][i] = 0; len[tot] = L; cnt[tot] = 0; return tot++;} void init(){ tot = 0; newnode(0); newnode (-1); last = 0; N = 0; str[0] = -1; fail[0] = 1;} int getfail(int x){ while(str[N-len[x]-1]! = str[N]) x = fail[x]; return x;} void add(char ss){ int c = ss-'a'; str[++N] = c; int cur = getfail(last); if(!nex[cur][c]){ int now = newnode(len[cur] + 2); fail[now] = nex[getfail(fail[cur])][c]; nex[cur][c ] = now; int need = (len[now] + 1) / 2; if(len[now] == 1 || getstring(N-len[now] + 1, N-len[now] + need) = = getstring(N-need + 1, N)) satisfy[now] = 1; else satisfy[now] = 0;} last = nex[cur][c]; cnt[last]++;} void count() {for(int i = tot-1; i >= 0; i--){ cnt[fail[i]] += cnt[i]; if(satisfy[i]) ans[len[i]] += cnt[i];} }}pa;char s[maxn];int main(){ fac[0] = 1; for(int i = 1; i 

Leave a Comment

Your email address will not be published.