题意翻译

给定一个长度为n的小写字母串。问你有多少对相交的回文子 串(包含也算相交) 。 输入格式

第一行是字符串长度n(1<=n<=2*10^6),第二行字符串 输出格式

相交的回文子串个数%51123987

Translated by liyifeng

题目描述

In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you that a string is called a palindrome if it can be read the same way both from left to right and from right to left. Here are examples of such strings: «eye», «pop», «level», «aba», «deed», «racecar», «rotor», «madam».

Nick started to look carefully for all palindromes in the text that they were reading in the class. For each occurrence of each palindrome in the text he wrote a pair — the position of the beginning and the position of the ending of this occurrence in the text. Nick called each occurrence of each palindrome he found in the text subpalindrome. When he found all the subpalindromes, he decided to find out how many different pairs among these subpalindromes cross. Two subpalindromes cross if they cover common positions in the text. No palindrome can cross itself.

Let's look at the actions, performed by Nick, by the example of text «babb». At first he wrote out all subpalindromes:

• «b» — 1..1 • «bab» — 1..3 • «a» — 2..2 • «b» — 3..3 • «bb» — 3..4 • «b» — 4..4 Then Nick counted the amount of different pairs among these subpalindromes that cross. These pairs were six:

  1. 1..1 cross with 1..3
  2. 1..3 cross with 2..2
  3. 1..3 cross with 3..3
  4. 1..3 cross with 3..4
  5. 3..3 cross with 3..4
  6. 3..4 cross with 4..4

    Since it's very exhausting to perform all the described actions manually, Nick asked you to help him and write a program that can find out the amount of different subpalindrome pairs that cross. Two subpalindrome pairs are regarded as different if one of the pairs contains a subpalindrome that the other does not.

输入输出格式

输入格式:

The first input line contains integer n ( 1<=n<=2·106 ) — length of the text. The following line contains nnlower-case Latin letters (from a to z).

输出格式:

In the only line output the amount of different pairs of two subpalindromes that cross each other. Output the answer modulo 51123987 .

输入输出样例

输入样例#1: 复制

4

babb

输出样例#1: 复制

6

输入样例#2: 复制

2

aa

输出样例#2: 复制

2


题解

一道比较不错的题目。

解法和P1872 回文串计数(回文树)差不多。

我们就是先统计一遍不会相交的情况,在统计一遍所有情况。

把所有情况减去不会相交的情况就可以了。

简单吗,简单。

一交代码

然后,你就会发现,你RE了。

让我们看一手数据。

1<=n<=2·106

空间复杂度铁定GG。出题人心机婊

所以我们用邻接表来代替trie的直接储存。

以时间换空间。Orz。


代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
ll tot,sum,num;
ll n,ans,p1[2000001],p2[2000001];
int head[2000001];
struct node{
int fail,len,cnt,dep;
}t[2000001];
struct nod{
int next,to,v;
}e[2000001];
char s[2000001];
const int mod=51123987;
int read()
{
int x=0,w=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*w;
} void link(int from,int to,int v){
num++;
e[num].to=to;
e[num].v=v;
e[num].next=head[from];
head[from]=num;
} int tr(int x,int v)
{
for(int i=head[x];i;i=e[i].next)
if(e[i].v==v)return e[i].to;
return 0;
} void solve1()
{
int len=strlen(s+1),k=0;s[0]='#';
t[0].fail=t[1].fail=1;t[1].len=-1;tot=1;
for(int i=1;i<=len;i++)
{
while(s[i-t[k].len-1]!=s[i])k=t[k].fail;
if(!tr(k,s[i]-'a')){
t[++tot].len=t[k].len+2;
int j=t[k].fail;
while(s[i-t[j].len-1]!=s[i])j=t[j].fail;
t[tot].fail=tr(j,s[i]-'a');
t[tot].dep=(t[t[tot].fail].dep+1)%mod;
link(k,tot,s[i]-'a');
}
k=tr(k,s[i]-'a');
p1[i]=(t[k].dep)%mod;
t[k].cnt++;
}
for(int i=tot;i>=2;i--)
t[t[i].fail].cnt+=t[i].cnt;
for(int i=tot;i>=2;i--)
if(t[i].len!=1)
sum+=t[i].cnt;
} void solve2()
{
int len=strlen(s+1),k=0;s[0]='#';
t[0].fail=t[1].fail=1;t[1].len=-1;tot=1;
for(int i=1;i<=len;i++)
{
while(s[i-t[k].len-1]!=s[i])k=t[k].fail;
if(!tr(k,s[i]-'a')){
t[++tot].len=t[k].len+2;
int j=t[k].fail;
while(s[i-t[j].len-1]!=s[i])j=t[j].fail;
t[tot].fail=tr(j,s[i]-'a');
t[tot].dep=(t[t[tot].fail].dep+1)%mod;
link(k,tot,s[i]-'a');
}
k=tr(k,s[i]-'a');
t[k].cnt++;
p2[len-i+1]=(t[k].dep)%mod;
}
} int main()
{
n=read();
scanf("%s",s+1);
int len=strlen(s+1);
solve1();
reverse(s+1,s+len+1);
memset(t,0,sizeof(t));
num=0;
memset(head,0,sizeof(head));
solve2();
sum%=mod;n%=mod;
for(int i=1;i<=len;i++)p1[i]+=p1[i-1],p1[i]%=mod;
for(int i=1;i<=len;i++)ans+=(p1[i]*p2[i+1])%mod,ans%=mod;
printf("%lld",(ll)((((sum+n)*(sum+n-1))/2%mod-ans)+mod)%mod);
return 0;
}

CF17E Palisection(回文树)的更多相关文章

  1. CodeForces 17E Palisection(回文树)

    E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard inpu ...

  2. CF17E Palisection (回文自动机+DP)

    题目传送门 题目大意:给你一个字符串,让你求出有多少对相交的回文子串 啊啊啊啊降智了,我怎么又忘了正难则反! 求相交会很难搞.把问题转化成求互不相交的回文子串再减一下就行了 先利用$PAM$求出以每个 ...

  3. 【CF17E】Palisection(回文树)

    [CF17E]Palisection(回文树) 题面 洛谷 题解 题意: 求有重叠部分的回文子串对的数量 所谓正难则反 求出所有不重叠的即可 求出以一个位置结束的回文串的数量 和以一个位置为开始的回文 ...

  4. CF17E Palisection(manacher/回文树)

    CF17E Palisection(manacher/回文树) Luogu 题解时间 直接正难则反改成求不相交的对数. manacher求出半径之后就可以差分搞出以某个位置为开头/结尾的回文串个数. ...

  5. Palisection(Codeforces Beta Round #17E+回文树)

    题目链接 传送门 题意 给你一个串串,问你有多少对回文串相交. 思路 由于正着做不太好算答案,那么我们考虑用总的回文对数减去不相交的回文对数. 而不相交的回文对数可以通过计算以\(i\)为右端点的回文 ...

  6. 回文树/回文自动机(PAM)学习笔记

    回文树(也就是回文自动机)实际上是奇偶两棵树,每一个节点代表一个本质不同的回文子串(一棵树上的串长度全部是奇数,另一棵全部是偶数),原串中每一个本质不同的回文子串都在树上出现一次且仅一次. 一个节点的 ...

  7. 回文树(回文自动机PAM)小结

    回文树学习博客:lwfcgz    poursoul 边写边更新,大概会把回文树总结在一个博客里吧... 回文树的功能 假设我们有一个串S,S下标从0开始,则回文树能做到如下几点: 1.求串S前缀0~ ...

  8. HDU3948 & 回文树模板

    Description: 求本质不同回文子串的个数 Solution: 回文树模板,学一学贴一贴啊... Code: /*================================= # Cre ...

  9. 2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树

    The Problem to Slow Down You Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjud ...

随机推荐

  1. Android 度量单位

    单位 注释 px(像素) 每个像素对应手机上的一个点,在不同设备上1px表示的长度不一定相同 screen size(屏幕尺寸) 指手机对角线的长度,如4.7英寸 resolution(分辨率) 指屏 ...

  2. Servlet学习(九)——request

    request运行流程在Servlet学习(四)——response已介绍,不再赘述 1.通过抓包工具获取Http请求 因为request代表请求,所以我们可以通过该对象分别获得Http请求的请求行, ...

  3. HOJ 1867 经理的烦恼 【 树状数组 】

    题意:给出一个区间,求这个区间里面素数的个数 这道题wa了好多次---是因为add操作没有写对 每次更新的时候,应该先判断没有加上y是不是质数,加上了y是不是质数 如果从质数变成不是质数,那么add( ...

  4. ActiveMQ学习笔记(20)----Consumer高级特性(二)

    1. Message Selectors JMS Selectors 用在获取消息的时候,可以基于消息属性和Xpath语法对消息进行过滤.JMS Selectors有SQL92语义定义.以下是个Sel ...

  5. Spark脚本调用

    Spark提供了多个脚本来作为程序的入口,其中最常用的是交互脚本 spark-shell, pyspark,还有spark sql的客户端spark-sql. 这些脚本最后都会归结到对SparkSub ...

  6. UNIX系统高级编程——第六章-系统数据文件和信息-总结

    口令文件: /* The passwd structure. */ struct passwd { char *pw_name; /* Username. */ char *pw_passwd; /* ...

  7. Ubuntu 16.04 Chrome浏览器安装flash player插件

    1:官网下载插件  flash palyer lash_player_npapi_linux_debug.x86_64.tar.gz 2:解压 提取 libpepflashplayer.so 3:手动 ...

  8. 紫书 习题8-3 UVa 12545 (构造法)

    参考了https://blog.csdn.net/catglory/article/details/47188949 最后推出来操作的个数为问号的个数 加上 同一位置上S串为0而T串为1的位置数量 与 ...

  9. Object-C学习比较费劲的3点原因

    作为一名有8年编程经验的资历不浅的程序员,学习Object-C和iOS,感觉效率有点低,至少和学习Android比起来,慢的不是一点点. 主要有以下3个原因: 1.Object-C,和C系列的语言(C ...

  10. 【BZOJ 1483】[HNOI2009]梦幻布丁

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 链表,启发式合并. 把x变成y,和y全都变成x. 不论是前者还是后者.连续段的个数都是相同的,不影响结果. 那么我们把x,y中出现次 ...