后缀回文自动机的学习链接:链接
因为自动机上每一个节点代表目前的串的一个回文串,所以只要统计当前自动机总共有o多少个节点就可以了。。
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005
#define maxm 2000005
#define eps 1e-10
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
//#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head
struct node
{
int len;
node *ch[26], *link;
}pool[maxn], *tot, *last;
char s[maxn];
int n;
void init(void)
{
memset(pool, 0, sizeof pool);
tot = &pool[2];
last = &pool[1];
pool[0].len = -1;
pool[1].link = &pool[0];
}
bool add(void)
{
char c = s[n-1];
node *p = last;
while(p && c != s[n - p->len - 2]) p = p->link;
bool res = false;
if(!p->ch[c]) {
res = true;
node *q = last = tot++;
q->len = p->len + 2;
p->ch[c] = q;
do p = p->link; while(p && c != s[n - p->len - 2]);
if(!p) q->link = &pool[1];
else q->link = p->ch[c];
}
else last = p->ch[c];
return res;
}
void work(void)
{
int ans = 0;
char c;
n = 0;
s[n++] = '#';
while((c = getchar()) != '\n') {
c -= 'a';
s[n++] = c;
ans += add();
printf("%d ", ans);
}
}
int main(void)
{
init();
work();
return 0;
}