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. 【html css js】实现一个简易日历

    ——[效果预览] 实现了日历最基础的功能,当前日期红色显示,可通过上方的左右按钮查看上一月或下一月的日期. ——[代码部分] 1. HTML <body> <div class=&q ...

  2. ajax传出数组到后台

    var vote = new Array();    $("input[name='option_name']").each(function(i){        if($(th ...

  3. windows系统cmd命令行窗口查看端口占用情况

    # 查看所有在用端口 netstat -ano # 查看指定端口 netstat -ano | findstr 8899 # 结束该进程:taskkill /f /t /im javaw.exe:或者 ...

  4. phpStudy中MySQL版本升级到5.7.17方法

    本文主要给大家介绍了关于phpStudy中升级MySQL版本到5.7.17的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧.希望能帮 ...

  5. 虚幻4 UE4 蓝图之关卡蓝图实现自动开关门

    新建项目 往关卡中放置一个门 在内容浏览器中找到 门 的静态网格体 拖放到关卡中 此时门默认没有碰撞,人物可以直接穿过 给门添加碰撞 双击内容管理器中的 SM_Door,打开编辑窗口 选择菜单&quo ...

  6. web自动化测试启示篇

    1.首先,对于想学自动化测试的朋友,那么你得懂一种语言,常用的比如Java或者Python.因为没有语言基础,你是写不出自动化脚本的. 我个人选择java 2.有了开发语言的铺垫,那么开始入手Sele ...

  7. opencv 5 图像转换(3 重映射 仿射变换 直方图均衡化)

    重映射 实现重映射(remap函数) 基础示例程序:基本重映射 //---------------------------------[头文件.命名空间包含部分]------------------- ...

  8. SpringBoot 正式环境必不可少的外部化配置

    前言 <[源码解析]凭什么?spring boot 一个 jar 就能开发 web 项目> 中有读者反应: 部署后运维很不方便,比较修改一个 IP 配置,需要重新打包. 这一点我是深有体会 ...

  9. bind cname

    $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS ...

  10. Flex利用JavaScript执行cmd命令

    Flex: //注册js事件            protected function init():void            {                ExternalInterfa ...