签到提;

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

题解:

用回文树直接暴力即可

回文树开一个数组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 ;
}

The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树的更多相关文章

  1. The Preliminary Contest for ICPC Asia Xuzhou 2019 G Colorful String(回文自动机+dfs)

    这题建立一棵回文树,然后用dfs搜索答案,但是有一点需要注意,就是打vis的标记时,如果标记为1,那么在好几个节点都对同一个字符i打过标记,此时的搜索从字符i点回溯,回到它的父亲节点,搜索其它的字符, ...

  2. 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)

    query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...

  3. The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team [单调栈上二分]

    也许更好的阅读体验 \(\mathcal{Description}\) 给n个数,与一个数m,求\(a_i\)右边最后一个至少比\(a_i\)大\(m\)的数与这个数之间有多少个数 \(2\leq n ...

  4. The Preliminary Contest for ICPC Asia Xuzhou 2019

    A:Who is better? 题目链接:https://nanti.jisuanke.com/t/41383 题意: 类似于有N个石子,先手第一次不能拿完,每次后手只能拿 1 到 前一次拿的数量* ...

  5. The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team

    题目链接:https://nanti.jisuanke.com/t/41387 思路:我们需要从后往前维护一个递增的序列. 因为:我们要的是wi + m <= wj,j要取最大,即离i最远的那个 ...

  6. 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛

    XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...

  7. The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING

    题意:给[1,n],n个数,有两种操作: 1 x,删去x2 x,查询还未被删去的数中大于等于x的最小的数是多少. input: output: 做法:按照并查集的方法压缩路径 代码: #include ...

  8. G.Colorful String(The Preliminary Contest for ICPC Asia Xuzhou 2019)

    https://nanti.jisuanke.com/t/4 #include <bits/stdc++.h> using namespace std; ,; typedef unsign ...

  9. E.XKC's basketball team(The Preliminary Contest for ICPC Asia Xuzhou 2019)

    https://nanti.jisuanke.com/t/41387 解: 离散化+线段树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); ...

随机推荐

  1. f-li.cn

    package org.rx.service.command.impl; import lombok.Getter; import lombok.Setter; import org.rx.core. ...

  2. opencv环境变量配置

    本文章由@浅墨_毛星云 出品  原文文章链接:http://blog.csdn.net/poem_qianmo/article/details/19809337 作者:毛星云(浅墨)    微博:ht ...

  3. DGIM算法

    /***************************************************** copyright (C), 2014-2015, Lighting Studio. Co ...

  4. PHP面试 PHP基础知识 六(正则表达式)

    正则表达式 正则表达式的作用 分割.查找.匹配.替换字符串 分隔符:正斜线(/).hash符号(#).以及取反符号(~)   通用原子:\d(代表十进制的0-9).\D (取反除了0-9).\w(数字 ...

  5. PHP简单读取XML

    DOM document object model,文档对象模型,就是树形结构 节点的分类 元素节点 属性节点 值节点 注释节点 根节点(document才是根节点) 生成XML文档 xml的根节点为 ...

  6. 前端(五)—— a、img、list标签

    a标签.img标签.list标签 一.a标签 1.常用用法 <a href="https://www.baidu.com">前往百度</a> <a h ...

  7. Python 变量作用域 LEGB (下)—— Enclosing function locals

    上篇:Python 变量作用域 LEGB (上)—— Local,Global,Builtin https://www.cnblogs.com/yvivid/p/python_LEGB_1.html ...

  8. 这是<一起找打的约定>的改良版本

    -- CREATE TABLE class ( -- cid INT(25)auto_increment PRIMARY KEY, -- caption VARCHAR(50) not NULL -- ...

  9. 49-Ubuntu-其他命令-1-文件软链接

    序号 命令 作用 01 ln -s 被链接的原文件 链接文件 建立文件的软链接,用通俗的方式讲类似于Windows下的快捷方式 注意: 没有-s选项建立的是一个硬链接文件--->>两个文件 ...

  10. JS点击2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...