签到提;

题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数)

题解:

用回文树直接暴力即可

回文树开一个数组cost[ ][26] 和val[ ] 数组;

val【i】表示回文树上节点 i 的对应的回文的贡献

最后统计答案即可

LL get_ans() {
LL ans = 0;
for (int i = sz - 1; i >= 0; --i) ans += 1LL * cnt[i] * val[i];
return ans;
}
 #include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sfi(a) scanf("%d", &a)
#define sffi(a, b) scanf("%d %d", &a, &b)
#define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define sfL(a) scanf("%lld", &a)
#define sffL(a, b) scanf("%lld %lld", &a, &b)
#define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define sfs(a) scanf("%s", a)
#define sffs(a, b) scanf("%s %s", a, b)
#define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
#define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
#define FIN freopen("../in.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL seed = ;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 4e5 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
char s[maxn]; struct Palindrome_Automaton {
int len[maxn], next[maxn][], fail[maxn], cnt[maxn], cost[maxn][], val[maxn];
int num[maxn], S[maxn], sz, n, last; int newnode(int l) {
for (int i = ; i < ; ++i)next[sz][i] = , cost[sz][i] = ;
cnt[sz] = num[sz] = , len[sz] = l;
return sz++;
} void init() {
sz = n = last = ;
newnode();
newnode(-);
S[] = -;
fail[] = ;
} int get_fail(int x) {
while (S[n - len[x] - ] != S[n])x = fail[x];
return x;
} void add(int c, int pos) {
c -= 'a';
S[++n] = c;
int cur = get_fail(last);
if (!next[cur][c]) {
int now = newnode(len[cur] + );
fail[now] = next[get_fail(fail[cur])][c];
next[cur][c] = now;
num[now] = num[fail[now]] + ; for (int i = ; i < ; i++) cost[now][i] = cost[cur][i];
cost[now][c] = ;
int temp = ;
for (int i = ; i < ; i++) temp += cost[now][i];
val[now] = temp; }
last = next[cur][c];
cnt[last]++;
} void count()//统计本质相同的回文串的出现次数
{
for (int i = sz - ; i >= ; --i)cnt[fail[i]] += cnt[i];
//逆序累加,保证每个点都会比它的父亲节点先算完,于是父亲节点能加到所有子孙
} LL get_ans() {
LL ans = ;
for (int i = sz - ; i >= ; --i) ans += 1LL * cnt[i] * val[i];
return ans;
}
} pam; int main() {
FIN;
sfs(s + );
pam.init();
int n = strlen(s + );
for (int i = ; i <= n; i++) {
pam.add(s[i], i);
}
pam.count();
LL ans = pam.get_ans();
printf("%lld\n", ans);
return ;
}
05-27 06:37