1. 程式人生 > >CF17E Palisection(回文樹)

CF17E Palisection(回文樹)

包含 right example and 子串 fin 我們 main h110

題意翻譯

給定一個長度為n的小寫字母串。問你有多少對相交的回文子 串(包含也算相交) 。 輸入格式

第一行是字符串長度n(1<=n<=2*10^6),第二行字符串 輸出格式

相交的回文子串個數%51123987

Translated by liyifeng

題目描述

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.

輸入輸出格式

輸入格式:

The first input line contains integer n ( 1<=n<=2·10^6^ ) — length of the text. The following line contains nnlower-case Latin letters (from a to z).

輸出格式:

In the only line output the amount of different pairs of two subpalindromes that cross each other. Output the answer modulo 51123987 .

輸入輸出樣例

輸入樣例#1: 復制

4
babb

輸出樣例#1: 復制

6

輸入樣例#2: 復制

2
aa

輸出樣例#2: 復制

2


題解

一道比較不錯的題目。
解法和P1872 回文串計數(回文樹)差不多。
我們就是先統計一遍不會相交的情況,在統計一遍所有情況。
把所有情況減去不會相交的情況就可以了。
簡單嗎,簡單。
一交代碼
然後,你就會發現,你RE了。
讓我們看一手數據。
1<=n<=2·10^6^
空間復雜度鐵定GG。出題人心機婊
所以我們用鄰接表來代替trie的直接儲存。
以時間換空間。Orz。


代碼

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
ll tot,sum,num;
ll n,ans,p1[2000001],p2[2000001];
int head[2000001];
struct node{
    int fail,len,cnt,dep;
}t[2000001];
struct nod{
    int next,to,v;
}e[2000001];
char s[2000001];
const int mod=51123987;
int read()
{
    int x=0,w=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*w;
}

void link(int from,int to,int v){
    num++;
    e[num].to=to;
    e[num].v=v;
    e[num].next=head[from];
    head[from]=num;
}

int tr(int x,int v)
{
    for(int i=head[x];i;i=e[i].next)
    if(e[i].v==v)return e[i].to;
    return 0;
}

void solve1()
{
    int len=strlen(s+1),k=0;s[0]='#';
    t[0].fail=t[1].fail=1;t[1].len=-1;tot=1;
    for(int i=1;i<=len;i++)
    {
        while(s[i-t[k].len-1]!=s[i])k=t[k].fail;
        if(!tr(k,s[i]-'a')){
            t[++tot].len=t[k].len+2;
            int j=t[k].fail;
            while(s[i-t[j].len-1]!=s[i])j=t[j].fail;
            t[tot].fail=tr(j,s[i]-'a');
            t[tot].dep=(t[t[tot].fail].dep+1)%mod;
            link(k,tot,s[i]-'a');
        }
        k=tr(k,s[i]-'a');
        p1[i]=(t[k].dep)%mod;
        t[k].cnt++;
    }
    for(int i=tot;i>=2;i--)
    t[t[i].fail].cnt+=t[i].cnt;
    for(int i=tot;i>=2;i--)
    if(t[i].len!=1)
    sum+=t[i].cnt;
}


void solve2()
{
    int len=strlen(s+1),k=0;s[0]='#';
    t[0].fail=t[1].fail=1;t[1].len=-1;tot=1;
    for(int i=1;i<=len;i++)
    {
        while(s[i-t[k].len-1]!=s[i])k=t[k].fail;
        if(!tr(k,s[i]-'a')){
            t[++tot].len=t[k].len+2;
            int j=t[k].fail;
            while(s[i-t[j].len-1]!=s[i])j=t[j].fail;
            t[tot].fail=tr(j,s[i]-'a');
            t[tot].dep=(t[t[tot].fail].dep+1)%mod;
            link(k,tot,s[i]-'a');
        }
        k=tr(k,s[i]-'a');
        t[k].cnt++;
        p2[len-i+1]=(t[k].dep)%mod;
    }
}


int main()
{
    n=read();
    scanf("%s",s+1);
    int len=strlen(s+1);
    solve1();
    reverse(s+1,s+len+1);
    memset(t,0,sizeof(t));
    num=0;
    memset(head,0,sizeof(head));
    solve2();
    sum%=mod;n%=mod;
    for(int i=1;i<=len;i++)p1[i]+=p1[i-1],p1[i]%=mod;
    for(int i=1;i<=len;i++)ans+=(p1[i]*p2[i+1])%mod,ans%=mod;
    printf("%lld",(ll)((((sum+n)*(sum+n-1))/2%mod-ans)+mod)%mod);
    return 0;
}

CF17E Palisection(回文樹)