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. 访问控制列表ACL

    1.ACL Access list ,访问控制列表. 2.作用 限制网络中的地址访问. 3.主要内容 Eg: Router(config)#access-list ? <一>. <1 ...

  2. BST的实现(二叉搜索树)

    void Inorder(struct Tree *T); //中序 void Preorder(struct Tree *T); //前序 void Postorder(struct Tree *T ...

  3. hdu 1233 (prim,最小生成树) 还是畅通工程

    还是畅通工程Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. nyoj 84-阶乘的0 (规律题)

    84-阶乘的0 内存限制:64MB 时间限制:3000ms 特判: No 通过数:7 提交数:9 难度:3 题目描述: 计算n!的十进制表示最后有多少个0 输入描述: 第一行输入一个整数N表示测试数据 ...

  5. ViewPage+Fragment的使用用法

    一.概述 从前面几篇文章,我们知道,实现ViewPager是要有适配器的,我们前面用的适配器是PagerAdapter,而对于fragment,它所使用的适配器是:FragmentPagerAdapt ...

  6. .NET Core 3.0之深入源码理解HealthCheck(一)

    写在前面 我们的系统可能因为正在部署.服务异常终止或者其他问题导致系统处于非健康状态,这个时候我们需要知道系统的健康状况,而健康检查可以帮助我们快速确定系统是否处于正常状态.一般情况下,我们会提供公开 ...

  7. scrapy抓取豆瓣电影相关数据

    1. 任务分析及说明 目标网站:https://movie.douban.com/tag/#/ 抓取豆瓣电影上,中国大陆地区,相关电影数据约1000条:数据包括:电影名称.导演.主演.评分.电影类型. ...

  8. windows和linux的开机顺序

    windows的开机顺序: 启动自检阶段---初始化启动阶段---Boot加载阶段---检测和配置硬件阶段---内核加载阶段---屏幕显示. linux的开机启动顺序: 加载Bios---读取MBR- ...

  9. Spring Cloud - Zuul添加过滤器

    Zuul作为网关的其中一个重要功能,就是实现请求的鉴权.而这个动作我们往往是通过Zuul提供的过滤器来实现的. 一.过滤器方法的作用 想要使用Zuul实现过滤功能,我们需要自定义一个类继承ZuulFi ...

  10. 5分钟上手自动化测试——Airtest+Poco快速上手

    版权声明:该文章为AirtestProject原创文章:允许转载,但转载必须注明“转载”并保留原链接 前言 本文档将演示使用`AirtestProject`专用的编辑器AirtestIDE,编写`Ai ...