题目链接:https://nanti.jisuanke.com/t/41389

The value of a string sss is equal to the number of different letters which appear in this string.

Your task is to calculate the total value of all the palindrome substring.

Input

The input consists of a single string ∣s∣(1≤∣s∣≤3×105)|s|(1 \le |s| \le 3 \times 10^5)∣s∣(1≤∣s∣≤3×105).

The string sss only contains lowercase letters.

Output

Output an integer that denotes the answer.

样例输入

abac

样例输出

6

样例解释

abac has palindrome substrings a,b,a,c,aba,ans the total value is equal to 1+1+1+1+2=6

题意:对于给定的字符串,求每个子回文串中不同的字符个数之和。

题解:回文树建树求出每不同的回文串出现的次数,再用DFS(开始节点,字母种类),累加求不同字符个数之和

#include<iostream>
#include<stdio.h>
#include <algorithm>
#include <string>
#include<string.h>
#include<math.h>
#define ll long long
using namespace std;
const int MAXN = ;
const int N = ;
char s[MAXN],ss[MAXN];//输入的要处理的字符串
ll ans=;
int vis[];
struct Palindromic_Tree
{
int next[MAXN][] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点 //回文树里面的一个节点就代表一个回文串
int cnt[MAXN] ;//表示第i个节点代表的回文串出现的次数
int num[MAXN] ; //表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数。
int len[MAXN] ;//表示第i个节点代表的回文串长度 int S[MAXN] ;//存放添加的字符
int last ;//指向上一个字符所在的节点,方便下一次add
int n ;//字符数组指针
int p ;//节点指针
int newnode(int l) //在树中新建节点
{
for(int i = ; i < N ; ++ i) next[p][i] = ;
cnt[p] = ;
num[p] = ;
len[p] = l ;
return p ++ ;
}
void init() //初始化
{
p = ;
newnode() ;//建一棵保存长度为偶数的回文树
newnode(-) ;//长度为奇数的回文树
last = ;
n = ;
S[n] = - ;//开头放一个字符集中没有的字符,减少特判
fail[] = ;
}
int get_fail(int x) //和KMP一样,失配后找一个尽量最长的
{
while(S[n - len[x] - ] != S[n])
x = fail[x] ;
return x ;
}
void add(int c,int pos)
{
//printf("%d:",p);//------------>输出节点编号,第一个有回文串的编号时从2开始
c -= 'a';
S[++ n] = c ;
int cur = get_fail(last) ; //通过上一个回文串找这个回文串的匹配位置
//printf("%d ",cur);//输出节点编号p代表的回文串的字符长度
if(!next[cur][c]) //如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
{
int now = newnode(len[cur] + ) ; //新建节点
fail[now] = next[get_fail(fail[cur])][c] ; //和AC自动机一样建立fail指针,以便失配后跳转
next[cur][c] = now ;
num[now] = num[fail[now]] + ;
}
last = next[cur][c] ;
cnt[last] ++ ;
//putchar(10);//------------->输出回车换行
}
void count()
{
for(int i = p - ; i >= ; -- i)
cnt[fail[i]] += cnt[i] ;
//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
}
void dfs(int x,int y)
{
for(int i=;i<;i++)
{
if(next[x][i])
{
int z;//回文串不同字母的个数
if(vis[i])//vis标记回文串出现字母的种类
{
z=y;
ans=ans+z*cnt[next[x][i]];
dfs(next[x][i],z);
}
else
{
vis[i]++;
z=y+;
ans=ans+z*cnt[next[x][i]];
dfs(next[x][i],z);
vis[i]--;//回溯
}
}
}
}
} pat;
int main()
{
ans=;
scanf("%s",s);
int n=strlen(s);
pat.init();
for(int i=; i<n; i++)
pat.add(s[i],i);
pat.count();
pat.dfs(,);//长度为偶数的回文树
memset(vis,,sizeof(vis));
pat.dfs(,);//长度为奇数的回文树
printf("%lld\n",ans);
return ;
}

2019 徐州网络赛 G Colorful String 回文树的更多相关文章

  1. The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树

    签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...

  2. ICPC 2019 徐州网络赛

    ICPC 2019 徐州网络赛 比赛时间:2019.9.7 比赛链接:The Preliminary Contest for ICPC Asia Xuzhou 2019 赛后的经验总结 // 比赛完才 ...

  3. HDU 5157 Harry and magic string(回文树)

    Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. hdu5421 Victor and String 回文树(前后插入)

    题目传送门 题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量. 思路: 就是在普通回文树的基础上,维护suf(最长回文后缀)的同时再维护一个pre(最 ...

  5. HDU 6599 I Love Palindrome String (回文树+hash)

    题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...

  6. ACM-ICPC 2018 南京赛区网络预赛 I. Skr(回文树)

    题意 https://nanti.jisuanke.com/t/A1955 求所有本质不同的回文串转成数后的和. 思路 如果了解回文树的构造原理,那么这题就很简单了,回文树每个结点代表一个回文串,每添 ...

  7. query 2019徐州网络赛(树状数组)

    query \[ Time Limit: 2000 ms \quad Memory Limit: 262144 kB \] 题意 补题才发现比赛的时候读了一个假题意.... 给出长度为 \(n\) 的 ...

  8. 2019南昌网络赛G. tsy's number

    题意:\(\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n\frac{\phi(i)*\phi(j^2)*\phi(k^3)}{\phi(i)*\phi(j)*\phi(k)} ...

  9. 2019徐州网络赛H :function (min25筛)

    题意:f(i)=i的幂次之和. 求(N+1-i)*f(i)之和. 思路:可以推论得对于一个素数p^k,其贡献是ans=(N+1)[N/(P^k)]+P^k(1+2+3...N/(P^k)); 我们分两 ...

随机推荐

  1. 【C语言】创建一个函数,判断某一正整数是否为水仙花数,并调用这个函数找出1000以内所有水仙花数

    #include <stdio.h> int fun(int x) { int a, b, c; a = x / ; b = x % / ; c = x % ; if (x == a * ...

  2. 对于javaScript中的alert和document.write()的执行顺序的记录

    代码: <script>//1----语句式的匿名函数创建以及调用 (function(name,age){ alert("name:"+name+",age ...

  3. QWidget::setLayout: Attempting to set QLayout "" on xxx "", which already has a layout

    QLayout是Qt应用开发中一个非常重要的组件,然而平时使用的时候不小心经常会发现控制台有类似如下的警告: QWidget::setLayout: Attempting to set QLayout ...

  4. NAT穿透的方式

    目前主要的NAT类型有如下几种: 1)Full-cone NAT, also known as one-to-one NAT 一旦一个内网地址 (iAddr:iPort) 被映射到一个外部地址 (eA ...

  5. 根据权限显示accordion

    前端界面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.asp ...

  6. 嵌入式大赛PPT

    题目:基于SLAM的移动机器人设计 嵌入式PPT应具有的几个部分 1.有哪些硬件 1)小车 2)STM32F429开发板 3)树莓派3b+开发板 4)4g通信模块 5)GPS模块 6)Kinect摄像 ...

  7. ETCD的常用命令

    Note that any key that was created using the v2 API will not be able to be queried via the v3 API. A ...

  8. IOS XE-show memory

    有些时候,我们可能会遇到IOS XE设备的high memory的情况.我们可以使用的命令去查看相关信息. 例如: Router# show version Router# show memory R ...

  9. 深入JAVA注解-Annotation(学习过程)

    JAVA注解-Annotation学习 本文目的:项目开发过程中遇到自定义注解,想要弄清楚其原理,但是自己的基础知识不足以支撑自己去探索此问题,所以先记录问题,然后补充基础知识,然后解决其问题.记录此 ...

  10. 计算机网络 - TCP/IP模型

    图片来自网上资料