Codeforces 17E Palisection


E. Palisection

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.

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


恶心死了

对于两个回文串的位置关系

我们只减少回文串右端点严格小于另一个回文串左端点的情况

所以两个串的关系最多只会被减少一次

需要排除没有意义的情况

还需要差分计算贡献,注意倒着跑

还需要单独考虑回文串和单个字符的交,还是找找规律求一个前缀和就好

前缀和:因为我们对于一个点,只求出了最长回文串,又因为这个回文串包含的小回文串(同一中心)的左右节点变化是每次加或碱2,所以可以前缀和求出


  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define N 2000010
  4. #define Mod 51123987
  5. #define LL long long
  6. int len,n;
  7. int r[N<<1],p[N<<1],psum[N<<1];
  8. char t[N],s[N<<1];
  9. void Manacher(){
  10. int id=0,pos=0,x;
  11. for(int i=1;i<n;i++){
  12. if(pos>i)x=min(p[id*2-i],pos-i+1);
  13. else x=1;
  14. while(s[i-x]==s[i+x])x++;
  15. x--;
  16. if(x+i>pos)pos=x+i,id=i;
  17. p[i]=x;
  18. }
  19. }
  20. int main(){
  21. scanf("%d%s",&len,t);
  22. s[n=0]='!';
  23. for(int i=0;i<len;i++)s[++n]='#',s[++n]=t[i];
  24. s[++n]='#';s[++n]='?';
  25. Manacher();
  26. int ans=0,tmp=0;
  27. for(int i=1;i<n;i++){
  28. if(p[i]==0&&s[i]=='#')continue;
  29. if(p[i]==1&&s[i]!='#')continue;
  30. //枚举左右边界,对当前回文串包含的所有回文串进行累加
  31. r[i-p[i]-1]--;r[i-1]++;//差分求贡献
  32. tmp=(tmp+p[i]/2)%Mod;
  33. }
  34. //r差分累加
  35. //r第一次累加 前缀和
  36. //r第二次累加 计算答案
  37. for(int i=n-1;i>0;i--)r[i]+=r[i+1];
  38. for(int i=n-1;i>=1;i--){
  39. if(i&1)r[i]=r[i+1];
  40. else r[i]=(r[i]+r[i+1])%Mod;
  41. }
  42. for(int i=n-3;i>0;i--)r[i]=(r[i]+r[i+2])%Mod;
  43. ans=1ll*tmp*(tmp-1)/2%Mod;
  44. for(int i=1;i<n;i++){
  45. if(p[i]==0&&s[i]=='#')continue;
  46. if(p[i]==1&&s[i]!='#')continue;
  47. ans-=r[i+3]-r[i+p[i]+3];
  48. ans=(ans%Mod+Mod)%Mod;
  49. }
  50. //讨论回文串和单个字符的交
  51. for(int i=2;i<n;i++)psum[i]=(i+psum[i-2])%Mod;
  52. for(int i=1;i<n;i++)ans=(ans+psum[p[i]])%Mod;
  53. printf("%d",ans);
  54. return 0;
  55. }

Codeforces 17E Palisection 【Manacher】的更多相关文章

  1. [CodeForces - 1225C]p-binary 【数论】【二进制】

    [CodeForces - 1225C]p-binary [数论][二进制] 标签: 题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory limit 5 ...

  2. 【manacher】HDU3068-最长回文

    [题目大意] 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. [manacher知识点] ①mx - i > P[j] 的时候,以S[j]为中心的回文子串 ...

  3. 【Manacher】Colorful String

    The value of a string s is equal to the number of different letters which appear in this string. You ...

  4. 【manacher】HDU4513-吉哥系列故事——完美队形II

    [题目大意] 求最长回文队伍且队伍由中间向两边递减. [思路] 和字符串一样的做法,在递推的时候增加判断条件:a[i-p[i]]<=a[i-p[i]+2]. #include<iostre ...

  5. Codeforces 17E Palisection - Manacher

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个串$s$询问,有多少对回文子串有交. 好像很简单的样子. 考虑能不能直接求,感觉有点麻烦.因为要考虑右端点在当前回文子串内还有区间包含 ...

  6. BZOJ2160 拉拉队排练【Manacher】

    Description 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛.所以作为拉拉队队长的楚雨荨同学知道,帮助篮球队训 ...

  7. Codeforces 490B Queue【模拟】

    题意还是很好理解的,根据题目给出描述条件然后求出这串QUEUE 我的做法就是用两个数组 before[] 和 after[] 表示 ai 前面的前面的人的学号 和 ai 后面的后面的人的学号 ex[] ...

  8. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】

    A. k-rounding time limit per test:1 second memory limit per test:256 megabytes input:standard input ...

  9. Codeforces 839C Journey【DFS】

    C. Journey time limit per test:2 seconds memory limit per test:256 megabytes input:standard input ou ...

随机推荐

  1. 如何优雅地使用 rm 防止误删除?

    IT 界的有一个老梗,一次某论坛的数据库管理员抱怨自己老板一直虐待他,结果他一气之下就删库跑路了...... 于是... 据新华社北京 8 月 20 日电 ,北京一软件工程师徐某离职后因公司未能如期结 ...

  2. RotateCard(自定义旋转view)

    使用方法Demo package com.example.displaydemo; import java.util.ArrayList; import com.example.displaydemo ...

  3. mongodb复制集开启安全认证

    之前我有一篇博客写的是“node.js通过权限验证连接MongoDB”,这篇博客上提到如何在启动文件中通过配置auth参数来开启权限认证,但这种认证方式只适合单机节点,当我们使用复制集时应该怎么开启权 ...

  4. 使用jQuery插件jRemoteValidate进行远程ajax验证,可以自定义返回的信息

    最近项目中有一个业务是收银员通过输入用户卡号,给用户充值或者消费,但是为了避免误操作(如卡号输错),于是编写了一个远程验证的jQuery插件, 当收银员输入卡号后,失去焦点,立即ajax请求服务器端, ...

  5. JS判断键盘上的上下左右键

    document.onkeydown=function(event){ var e = event || window.event || arguments.callee.caller.argumen ...

  6. 常见浏览器bug(针对IE6及更低版本)及其修复方法

    常见bug及其修复方法有以下几种 1.双外边距浮动bug 双外边距浮动bug在IE6及更低版本中常见.所谓双外边距浮动bug是指使任何浮动元素上的外边距加倍.(见下图) 只要将元素的display属性 ...

  7. taskset -pc PID 查看线程占用cpu核

    taskset -pc  PID 可以用于 查看 当前线程 对应绑定的 在 哪个核上面. 这个 可以用于 程序优化, 查看 哪个线程占用的 cpu 比重比较高 首先 可以通过  top  -H   - ...

  8. py-faster-rcnn:在windows上配置

    0.先说一下本机配置 opencv2+cuda7.5+cudnn+anaconda,这些基础的之前都是配置好了的,python环境建议使用anaconda,用到的库基本都有了,好像没有easydict ...

  9. 12.18 分布式系统下的session

    广义的session: 会话控制,可以理解成为一种保存key-value的机制 从key的方面来看:sessionId和token sessionId: 服务端请求客户端的时候,服务端通过setcoo ...

  10. NODE 开发 2-3年工作经验 掌握的相关知识

    文章 部分答案 内存