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. css3mask淡出文字

    css3mask淡出文字<pre>.p0_11wrap { width: 0.299rem; height: 7.68rem; overflow: hidden; background: ...

  2. 使用客户机和主机做DNS服务正向解析及小问题解决

    1.下载yum包 命令:yum install bind-chroot 2.更改配置文件 在这里,要了解到主配置文件为:   /etc/named.conf 但是,为了避免经常修改主配置文件named ...

  3. CentOS7.6手动编译httpd-2.4.25

    手动编译httpd-2.4.25 系统:CentOS7.1810 httpd:2.4.25 编译时报错解决技巧:报什么错,就装这个错误的devel,比如报http2错误,就yum search htt ...

  4. Angular前端优化思路

    简单总结接下我这边angular前端优化步骤都是满满的干货,各位客官有好的改进欢迎留言~ 1. 动静分离 项目里面前端比较占用带宽的一般都是加载静态资源,请求后台接口一般占用带宽都是1kb左右,但是在 ...

  5. 读《MySQL必知必会》我学到了什么?

    前言 最近在写项目的时候发现自己的SQL基本功有些薄弱,遂上知乎查询MYSQL关键字,期望得到某些高赞答案的指点,于是乎发现了 https://www.zhihu.com/question/34840 ...

  6. 【原创】使用批处理脚本生成包并自动上传到nuget

    Hello 大家好,我是TANZAME,我们又见面了. NuGet 是什么这里就不再重复啰嗦,园子里一搜一大把.今天要跟大家分享的是,在日常开发过程中如何统一管理我们的包,如何通过批处理脚本生成包并自 ...

  7. 网站搭建-IIS Windows系统搭建网站 (不小心看到自己的密码 - 怎么找回网站记住的密码)

    上一期说到IIS可以用自己喜欢的网站来直接玩,然后得得瑟瑟将自己的博客园账号首页拿过去玩(今天第一天水博客园). 然后自己访问啊,访问啊,然后就一直点啊点的,当然,其实后面的链接都是跳转到博客园里面去 ...

  8. tornado的使用-数据库篇

    tornado的使用-数据库篇

  9. 破解加密Excel

    打开要解除保护的EXCEL ALT+F11----插入模块----复制粘贴代码----F5============================代码========================= ...

  10. 使用Spring安全表达式控制系统功能访问权限

    一.SPEL表达式权限控制 从spring security 3.0开始已经可以使用spring Expression表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限.Spring ...