E. Palisection
time limit per test

2 seconds

memory limit per test

128 megabytes

input

standard input

output

standard output

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
大致题意:求相交的回文子串有多少个.
分析:相交的回文子串的数量=总数量-不相交的回文子串的数量,这里的总数量指的是有多少对.那么如何求不相交的回文子串的数量呢?比较常见的套路,类似bzoj2565,将中心转移到两边,也就是记录以i为左端点结尾的回文子串有多少个,以i为右端点结尾的回文子串有多少个.那么每处理到一个i,就要更新i-r[i]到i的左端点值,i到i+r[i]的右端点值,很多次区间操作,可以用差分快速维护.
Manacher算法非常容易错的就是插入字符的讨论.最后答案的统计肯定实在实际字符上的,要将插入字符的答案转移到实际字符上来,对此要进行分类讨论,看这一位到底是实际字符还是插入字符.差分完后对实际字符做一次前缀和.这样可以得到每个的值.再对右端点做一次后缀和.这样就可以通过乘法原理和加法原理累计答案了.
Manacher算法的经典变形:中间变到两边. 易错点:对+1,-1的考虑,插入字符的分类讨论. 要点:统计答案是统计实际字符的答案.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; const int mod = ;
ll n,len,r[],p,mx,f[],g[],ans;
char s[],ss[]; void solve()
{
mx = p = ;
for (int i = ; i <= len; i++)
{
ll x = ;
if (i < mx)
x = min(r[ * p - i],mx - i);
else
x = ;
while (ss[i + x] == ss[i - x])
x++;
r[i] = x;
if (i + x > mx)
{
mx = i + x;
p = i;
}
ans += (x - ) / ;
if (i % == )
ans++;
ans %= mod;
}
ans = (ans - ) * ans / ;
ans %= mod;
} int main()
{
cin >> n;
scanf("%s",s + );
ss[] = '(';
for (int i = ; i <= n; i++)
{
ss[++len] = '#';
ss[++len] = s[i];
}
ss[++len] = '#';
ss[len + ] = ')';
solve();
for (int i = ; i <= len; i += )//差分修改的实际上是回文半径覆盖到的最左边的字符和最右边的字符的右边第一个字符(实际字符)
{
f[i - r[i] + ]++;
f[i + ]--;
g[i]++;
g[i + r[i]]--;
}
for (int i = ; i <= len; i += )
{
f[i - r[i] + ]++;
f[i + ]--;
g[i + ]++;
g[i + r[i]]--;
}
for (int i = ; i <= len; i += )
{
f[i] += f[i - ];
f[i] %= mod;
g[i] += g[i - ];
g[i] %= mod;
}
f[len + ] = ;
for(int i = len - ; i >= ; i -= )
{
f[i] += f[i + ];
f[i] %= mod;
}
f[len + ] = ;
for (int i = ; i <= len; i += )
{
ans -= g[i] * f[i + ] % mod;
ans = (ans + mod) % mod;
}
printf("%I64d\n",(ans % mod + mod) % mod); return ;
}

 

Codeforces 17.E Palisection的更多相关文章

  1. 【Codeforces 17E】Palisection

    Codeforces 17 E 题意:给一个串,求其中回文子串交叉的对数. 思路1:用哈希解决.首先求出每个点左右最长的回文串(要分奇数长度和偶数长度),然后记录经过每个点的回文串的个数,以及它们是在 ...

  2. Codeforces Beta Round #17 C. Balance DP

    C. Balance 题目链接 http://codeforces.com/contest/17/problem/C 题面 Nick likes strings very much, he likes ...

  3. Codeforces Beta Round #17 A - Noldbach problem 暴力

    A - Noldbach problem 题面链接 http://codeforces.com/contest/17/problem/A 题面 Nick is interested in prime ...

  4. Codeforces 17E Palisection 【Manacher】

    Codeforces 17E Palisection E. Palisection In an English class Nick had nothing to do at all, and rem ...

  5. Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)

    Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...

  6. Educational Codeforces Round 17

    Educational Codeforces Round 17 A. k-th divisor 水题,把所有因子找出来排序然后找第\(k\)大 view code //#pragma GCC opti ...

  7. Codeforces 17E Palisection - Manacher

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

  8. CodeForces 17E Palisection(回文树)

    E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard inpu ...

  9. Educational Codeforces Round 17 颓废记

    又被虐了... (记一次惨痛的Codeforces) 好不容易登上去了Codeforces,22:35准时开打 第一题,一看:这不SB题嘛?直接枚举因数上啊.9min才过掉了pretest 第二题.. ...

随机推荐

  1. 剑指 Offer——数字在排序数组中出现的次数

    1. 题目 2. 解答 时间复杂度为 \(O(n)\) 的算法,顺序遍历数组,当该数字第一次出现时开始记录次数. class Solution { public: int GetNumberOfK(v ...

  2. Wacom发布Cintiq Companion 2

    全新的Cintiq Companion 2是一款强大的平板电脑,让创意专业人士获得最佳的屏幕笔触,让创意随时随地进行.用户还可以在家中或工作时连接到Mac或PC电脑获得无与伦比的灵活性! 2015年1 ...

  3. java调试器

    javac.exe是编译.java文件 java.exe是执行编译好的.class文件 javadoc.exe是生成Java说明文档 jdb.exe是Java调试器 javaprof.exe是剖析工具 ...

  4. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  5. Alpha-6

    前言 失心疯病源6 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 今天完成了那些任务 18:30~20:30 完成blob类下关于预测车辆下一个位置的函数 代码签入github ...

  6. 使用Python 、 go 语言测试rabbitmq的工作机制

    1:在haproxy 和 rabbitmq上安装Python.python2-pip,默认是Python2 yum install -y python python2-pip   2:在haproxy ...

  7. 软工网络15团队作业4-DAY4

    每日立会 昨天的工作. 张陈东芳:sql语句存储商品信息 吴敏烽:调试获取商品信息的方法 周汉麟:根据商品编号来获取商品资料方法调试 林振斌:输出最近浏览记录的方法检查 李智:cookies的检查 全 ...

  8. phaser3 微信小游戏若干问题

    纯属个人兴趣, 如有兴趣可共同参与维护. git: https://gitee.com/redw1234567/phaser3_wx image的地方需要修改,代码贴上 var ImageFile = ...

  9. postman md5加密 然后传给下一个接口作为参数调用

    https://www.v2ex.com/api/nodes/show.json?name=python get请求 断言: tests["Body matches string" ...

  10. IE8 没有内容的盒子,如果有定位,浮现在其他盒子上 可能会有点击穿透没有作用的情况

    IE8 没有内容的盒子,如果有定位,浮现在其他盒子上 可能会有点击穿透没有作用的情况