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..32. 1..3 cross with 2..23. 1..3 cross with 3..34. 1..3 cross with 3..45. 3..3 cross with 3..46. 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.

Input

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

Output

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

Examples

Input
4
babb
Output
6
Input
2
aa
Output
2
题解:题目意思是让你求一个字符串内的相交的回文子串对个数。
(相交=> n*(n-1)/2 -不相交)
问题转化为求不相交子串。
我们可正着跑一遍PAM,求出某个字符前面的回文子串个数,倒着跑一遍PAM求出某个字符后面的回文子串个数,相乘就是该位置的贡献。
然后用总的答案减去它就行了。
 
参考代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 2e6+;
const int mod = ;
int n,fa[N],len[N],dep[N],tot,last,p1[N],p2[N],ans;
int to[N],nxt[N],ww[N],head[N],cnt;
char s[N];
void init()
{
fa[last=]=fa[]=;
len[]=;len[tot=]=-;
memset(head,,sizeof(head));cnt=;
}
void link(int u,int v,int c)
{
to[++cnt]=v;nxt[cnt]=head[u];ww[cnt]=c;
head[u]=cnt;
}
int tr(int v,int c)
{
for(int e=head[v];e;e=nxt[e])
if(ww[e]==c) return to[e];
return ;
}
void extend(int c,int n)
{
int v=last;
while(s[n-len[v]-]!=s[n]) v=fa[v];
if(!tr(v,c))
{
int u=++tot,k=fa[v];
len[u]=len[v]+;
while(s[n-len[k]-]!=s[n]) k=fa[k];
fa[u]=tr(k,c); dep[u]=dep[fa[u]]+;
link(v,u,c);
}
last=tr(v,c);
}
int main()
{
scanf("%d",&n);
scanf("%s",s+);
init();
for(int i=;i<=n;++i) extend(s[i]-'a',i),(ans+=(p1[i]=dep[last]))%=mod;
ans=1ll*ans*(ans-)/%mod;
reverse(s+,s+n+);
init();
for(int i=;i<=n;++i) extend(s[i]-'a',i),p2[n-i+]=dep[last];
for(int i=n;i;--i) (p2[i]+=p2[i+])%=mod;
for(int i=;i<=n;++i) ans=(ans-1ll*p1[i]*p2[i+]%mod+mod)%mod;
printf("%d\n",ans);
return ;
}

CF 17E Palisection 求相交回文串个数的更多相关文章

  1. 牛客练习赛64 如果我让你查回文你还爱我吗 线段树 树状数组 manacher 计数 区间本质不同回文串个数

    LINK:如果我让你查回文你还爱我吗 了解到了这个模板题. 果然我不会写2333... 考试的时候想到了一个非常辣鸡的 线段树合并+莫队的做法 过不了不再赘述. 当然也想到了manacher不过不太会 ...

  2. HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

  3. 3676: [Apio2014]回文串 求回文串长度与出现次数的最大值

    「BZOJ3676」[Apio2014] 回文串   Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所 ...

  4. 【bzoj3676】[Apio2014]回文串 —— 回文自动机的学习

    写题遇上一棘手的题,[Apio2014]回文串,一眼看过后缀数组+Manacher.然后就码码码...过是过了,然后看一下[Status],怎么慢这么多,不服..然后就搜了一下,发现一种新东西——回文 ...

  5. HDU3068 最长回文串

    题目大意:给出一个字符串,求其回文串的长度.有多组数据. 分析:manacher算法模板题. //在原字符串两边和中间插入一个从未出现的字符,比如‘#’.然后再在最前面插入一个‘*’.#include ...

  6. 【hdu3948-不同回文串的个数】后缀数组

    题意:求不同回文串的个数 n<=10^5 题解: 先按照manacher的构造方法改造一遍串,然后跑一遍manacher. 如ababa--> $#a#b#a#b#a#@ 然后跑一遍后缀数 ...

  7. Uva 11584,划分成回文串

    题目链接:https://uva.onlinejudge.org/external/115/11584.pdf 题意: 一个字符串,将它划分一下,使得每个串都是回文串,求最少的回文串个数. 分析: d ...

  8. BZOJ3676[Apio2014]回文串——回文自动机

    题目描述 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. 输入 输入只有一行,为一个只包含小写字 ...

  9. BZOJ2565:最长双回文串——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2565 题目大意: 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(ab ...

随机推荐

  1. 004.Kubernetes二进制部署创建证书

    一 创建CA证书和密钥 1.1 安装cfssl工具集 [root@k8smaster01 ~]# mkdir -p /opt/k8s/cert [root@k8smaster01 ~]# curl - ...

  2. php查询字符串的函数

    /* 查找一个字符串在另一个字符串的第一次出现,并返回其余部分(strstr别名) */ var_dump(strchr("hello world hello", "wo ...

  3. AI的真实感

    目录 1.让AI"不完美"--估算和假设 2 AI感知 全能感知 特定感觉无知 3 AI的个性 4 AI的预判 5 AI的智能等级 ​ AI的真实感一直是游戏AI程序员追求的目标, ...

  4. 源码学习系列之SpringBoot自动配置(篇二)

    源码学习系列之SpringBoot自动配置(篇二)之HttpEncodingAutoConfiguration 源码分析 继上一篇博客源码学习系列之SpringBoot自动配置(篇一)之后,本博客继续 ...

  5. MySQL添加主键和外键

    查看表的字段信息:desc 表名; 查看表的所有信息:show create table 表名; 添加主键约束:alter table 表名 add constraint 主键 (形如:PK_表名) ...

  6. bash6——循环

    for fruit in apple orange pear #写死 do each ${fruit}s done fruits="apple orange pear" #输入变量 ...

  7. 理解Spark SQL(三)—— Spark SQL程序举例

    上一篇说到,在Spark 2.x当中,实际上SQLContext和HiveContext是过时的,相反是采用SparkSession对象的sql函数来操作SQL语句的.使用这个函数执行SQL语句前需要 ...

  8. canvas入门,就是这个feel!

    钙素 Canvas 是在HTML5中新增的标签用于在网页实时生成图像,并且可以操作图像内容,基本上它是一个可以用JavaScript操作的位图.也就是说我们将通过JS完成画图而不是css. canva ...

  9. Java从零到企业级电商项目实战(第1章 课程介绍)

  10. linux计算机网络基础

    OSI7层协议和TCP/IP4层网络协议 第一层:物理层,定义各种物理设备的规范,如通信距离,接口大小等. 第二层:数据链路层,基于mac地址通信是,数据报文封装和相应方式. 第三层:网络层,基于IP ...