求以每个节点结尾的,回文子串的个数,最大回文子串的长度
求回文串的总个数(必须连续)
不连续的是动态规划
#include<bits/stdc++.h>
using namespace std;
const int maxn = 500005;
char str[maxn];
struct PAM {
int size, last, r0, r1;
int trie[maxn][26], fail[maxn], len[maxn], cnt[maxn];
PAM() {
r0 = size++, r1 = size++; last = r1;
len[r0] = 0, fail[r0] = r1;
len[r1] = -1, fail[r1] = r1;
}
void insert(int ch, int idx) {
int u = last;
while (str[idx] != str[idx - len[u] - 1])u = fail[u];
if (!trie[u][ch]) {
int cur = ++size, v = fail[u];
len[cur] = len[u] + 2;
for (; str[idx] != str[idx - len[v] - 1]; v = fail[v]);
fail[cur] = trie[v][ch]; trie[u][ch] = cur;
cnt[cur] = cnt[fail[cur]] + 1;
}
last = trie[u][ch];
}
void build(char* str) {
int len = strlen(str);
for (int i = 0; i < len; i++) {
insert(str[i] - 'a' + 1, i);
//printf("%d ", cnt[last]);//在这个位置打印以每个节点为结尾的,字符串个数
}
}
}pam;
int main(){
scanf("%s", str);
pam.build(str);
/*这个位置来打印各个节点的次数也可以,但是要从3开始*/
// for(int i=3;i<=strlen(str)+2;i++)
// {
// cout<<pam.cnt[i]<<endl;
// }
return 0;
}
标签:r1,int,cur,len,str,fail,自动机,回文
From: https://www.cnblogs.com/yzzyang/p/18151114