Codeforces 17.E Palisection

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 = 51123987;
ll n,len,r[5000010],p,mx,f[5000010],g[5000010],ans;
char s[2000010],ss[5000010];

void solve()
{
    mx = p = 1;
    for (int i = 1; i <= len; i++)
    {
        ll x = 0;
        if (i < mx)
            x = min(r[2 * p - i],mx - i);
        else
            x = 1;
        while (ss[i + x] == ss[i - x])
            x++;
        r[i] = x;
        if (i + x > mx)
        {
            mx = i + x;
            p = i;
        }
        ans += (x - 1) / 2;
        if (i % 2 == 0)
            ans++;
        ans %= mod;
    }
    ans = (ans - 1) * ans / 2;
    ans %= mod;
}

int main()
{
    cin >> n;
    scanf("%s",s + 1);
    ss[0] = '(';
    for (int i = 1; i <= n; i++)
    {
        ss[++len] = '#';
        ss[++len] = s[i];
    }
    ss[++len] = '#';
    ss[len + 1] = ')';
    solve();
    for (int i = 2; i <= len; i += 2)//差分修改的实际上是回文半径覆盖到的最左边的字符和最右边的字符的右边第一个字符(实际字符)
    {
        f[i - r[i] + 2]++;
        f[i + 2]--;
        g[i]++;
        g[i + r[i]]--;
    }
    for (int i = 1; i <= len; i += 2)
    {
        f[i - r[i] + 2]++;
        f[i + 1]--;
        g[i + 1]++;
        g[i + r[i]]--;
    }
    for  (int i = 2; i <= len; i += 2)
    {
        f[i] += f[i - 2];
        f[i] %= mod;
        g[i] += g[i - 2];
        g[i] %= mod;
    }
    f[len + 1] = 0;
    for(int i = len - 1; i >= 1; i -= 2)
    {
         f[i] += f[i + 2];
         f[i] %= mod;
    }
    f[len + 2] = 0;
    for (int i = 2; i <= len; i += 2)
        {
            ans -= g[i] * f[i + 2] % mod;
            ans = (ans + mod) % mod;
        }
    printf("%I64d\n",(ans % mod + mod) % mod);

    return 0;
}

 

 
posted @ 2017-12-19 21:51  zbtrs  阅读(283)  评论(0编辑  收藏  举报