如果会AC自动机的话,其实可以发现和回文自动机几乎是同一个东西,原理都差不多,只不过回文树的节点存的是一个回文串。
fail指针也是,指向当前节点的最长回文后缀。当新来一个字符的时候就匹配当前节点,如果当前节点的另外一头和新加的字符相同,那么就可以往下建字典树新节点。
以下是回文树几个基础练习题:
The 2014 ACM-ICPC Asia Xi’an Regional Contest
Problem G. The Problem to Slow Down You
题目大意:给俩字符串,找到有多少对相同的回文串。
有了回文树的话就简单多了,我们知道回文树的每一个节点都代表一个回文串,我们得到cnt数组之后也就是当前节点的回文串有多少(从fail父亲转儿子,大串包含小串)然后dfs两棵树有相同的节点说明有相同的回文串,cnt相乘记录答案。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 2e5+5;
const int N = 26 ;
struct Palindromic_Tree {
int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
ll cnt[MAXN] ;
int num[MAXN] ;
int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
int S[MAXN] ;//存放添加的字符
int last ;//指向上一个字符所在的节点,方便下一次add
int n ;//字符数组指针
int p ;//节点指针
int newnode ( int l ) {//新建节点
for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ;
cnt[p] = 0 ;
num[p] = 0 ;
len[p] = l ;
return p ++ ;
}
void init () {//初始化
p = 0 ;
newnode ( 0 ) ;
newnode ( -1 ) ;
last = 0 ;
n = 0 ;
S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
fail[0] = 1 ;
}
int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的
while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ;//,cout<<x<<" * "<<endl;;
return x ;
}
void add ( int c ) {
c -= 'a' ;
S[++ n] = c ;
int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
int now = newnode ( len[cur] + 2 ) ;//新建节点
fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
next[cur][c] = now ;
num[now] = num[fail[now]] + 1 ;
}
last = next[cur][c] ;
cnt[last] ++ ;
}
void Count () {
for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
}
} ;
char s1[MAXN],s2[MAXN];
ll ans = 0;
Palindromic_Tree A,B;
void dfs(int x,int y)
{
for(int i=0;i<26;i++){
if(A.next[x][i] && B.next[y][i]){
int dx = A.next[x][i];
int dy = B.next[y][i];
ans += A.cnt[dx] * B.cnt[dy];
dfs(dx,dy);
}
}
}
int main()
{
int t;
scanf("%d",&t);
int ss = 0;
while(t--){
A.init();B.init();
scanf("%s%s",s1,s2);
int len1 = strlen(s1);
int len2 = strlen(s2);
for(int i=0;i<len1;i++){
A.add(s1[i]); //cout<<" + "<<endl;
}
for(int i=0;i<len2;i++){
B.add(s2[i]);
}
A.Count();B.Count();
ans = 0;
dfs(0,0);
dfs(1,1);
printf("Case #%d: %I64d\n",++ss,ans);
}
}
题目给了10s , 回文树217ms跑的飞快
ural1960. Palindromes and Super Abilities
题目大意:一个字符串,每插入一个字符就可以出现多少个本质不同的回文串。
题目思路:建立回文树,插入一个节点,就多一个回文串。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 2e5+5;
const int N = 26 ;
struct Palindromic_Tree {
int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
ll cnt[MAXN] ;
int num[MAXN] ;
int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
int S[MAXN] ;//存放添加的字符
int last ;//指向上一个字符所在的节点,方便下一次add
int n ;//字符数组指针
int p ;//节点指针
int newnode ( int l ) {//新建节点
for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ;
cnt[p] = 0 ;
num[p] = 0 ;
len[p] = l ;
return p ++ ;
}
void init () {//初始化
p = 0 ;
newnode ( 0 ) ;
newnode ( -1 ) ;
last = 0 ;
n = 0 ;
S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
fail[0] = 1 ;
}
int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的
while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ;//,cout<<x<<" * "<<endl;;
return x ;
}
void add ( int c ) {
c -= 'a' ;
S[++ n] = c ;
int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
int now = newnode ( len[cur] + 2 ) ;//新建节点
fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
next[cur][c] = now ;
num[now] = num[fail[now]] + 1 ;
}
last = next[cur][c] ;
cnt[last] ++ ;
}
void Count () {
for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
}
} ;
Palindromic_Tree A;
char s1[MAXN];
int main()
{
A.init();
scanf("%s",s1);
int len = strlen(s1);
for(int i=0;i<len;i++){
A.add(s1[i]);
printf("%d%c",A.p-2,(i==len-1)?'\n':' ');
}
}
A1280. 最长双回文串
题目大意:找最长的一个字串,让他可以分成俩回文串。
题目思路:回文树的len数组就是当前节点的最长回文串长度。所以正向跑一遍回文树,反向跑一边回文树就可以了。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 2e5+5;
const int N = 26 ;
struct Palindromic_Tree {
int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
ll cnt[MAXN] ;
int num[MAXN] ;
int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
int S[MAXN] ;//存放添加的字符
int last ;//指向上一个字符所在的节点,方便下一次add
int n ;//字符数组指针
int p ;//节点指针
int newnode ( int l ) {//新建节点
for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ;
cnt[p] = 0 ;
num[p] = 0 ;
len[p] = l ;
return p ++ ;
}
void init () {//初始化
p = 0 ;
newnode ( 0 ) ;
newnode ( -1 ) ;
last = 0 ;
n = 0 ;
S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
fail[0] = 1 ;
}
int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的
while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ;//,cout<<x<<" * "<<endl;;
return x ;
}
void add ( int c ) {
c -= 'a' ;
S[++ n] = c ;
int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
int now = newnode ( len[cur] + 2 ) ;//新建节点
fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
next[cur][c] = now ;
num[now] = num[fail[now]] + 1 ;
}
last = next[cur][c] ;
cnt[last] ++ ;
}
void Count () {
for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
}
} ;
char s1[MAXN];
int a[MAXN];
Palindromic_Tree A;
int main()
{
A.init();
scanf("%s",s1);
int len = strlen(s1);
for(int i=0;i<len;i++){
A.add(s1[i]);
a[i] = A.len[A.last];
}
int ans = 0;
A.init();
for(int j=len-1;j>=0;j--){
A.add(s1[j]);
ans = max(ans,a[j-1]+A.len[A.last]);
}
cout<<ans<<endl;
}