Codeforces 17E palisection (palindrome tree)

Source: Internet
Author: User

E. palisectiontime limit per test2 secondsmemory limit per test128 megabytesinputstandard inputoutputstandard output

In an 中文版 class Nick had nothing to does at all, and remembered about wonderful strings calledpalindromes. We should remind you a string was called a palindrome if it can be read the same A-both from left Right-to-left. Here is examples of such strings:? Eye?, ?Pop?, ? Level?, ?ABA?, ?Deed?, ?Racecar?, ?Rotor?, ?Madam?.

Nic K started to look carefully for all palindromes in the text, they were reading in the class. For each occurrence of all 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. 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 ? ? The 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 this 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-help him and write a program T Hat can find out the amount of different subpalindrome pairs. Subpalindrome pairs is regarded as different if one of the pairs contains a subpalindrome that the other does not.

Input

The first input line contains integerN(1?≤? n? ≤?2 106 )-length of the text. The following line containsNlower-case Latin Letters (fromaToZ).

Output

In the-line output the amount of different pairs of the subpalindromes of the. Output the answer modulo 51123987.

Examplesinput
4babb
Output
6
Input
2aa
Output
2
Give you a string to ask, the number of all intersecting palindrome pairs
We can use the palindrome tree to find out the number of palindrome substring ending with s[i], and the number of palindrome strings starting with s[i.
Then the disjoint palindrome string pair is Ai*sum{bi+1,bi+2,bi+3....bi+n}  {i=1..n-1};
Then the total palindrome string minus the disjoint is the answer.
We know that the palindrome tree can be used to find the number of palindrome at the end of I, then start with I, just want to insert the string backwards.
In addition, the string length of this topic has 2 million, palindrome tree next[][] Array, will be memory overrun, so can only use adjacency table instead of matrix
In order to better learn the palindrome tree, in the case of all palindrome string and we can use two ways, specific look at the code
#include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> #include < Math.h> #include <stdio.h>using namespace std;typedef long long int ll;const int Max=2*1e6;const int mod=51123987    ; int N;char str[max+5];int sum[max+5];struct link//next adjacency table {int u[max+5];int v[max+5];    int Next[max+5];int head[max+5];    int tot;        void Clear () {memset (head,-1,sizeof (head));    tot=0;    } void Clear (int x) {head[x]=-1;} int get (int x,int y) {for (int i=head[x];i!=-1;i=next[i]) {if (u[i]==y) return        V[i];    } return 0;  } void Insert (int x,int y,int z) {u[tot]=y;        V[tot]=z;        NEXT[TOT]=HEAD[X];    head[x]=tot++;    }};struct tree{//int next[max+5][26]; int fail[max+5];//to the longest suffix of the current node palindrome string int cnt[max+5];//The current node's palindrome string how many int num[max+5];//the number of palindrome substrings at the end of the current node int len[ma   x+5];//the length of the palindrome string of the current node int s[max+5];//string int last;//palindrome tree last node int n; int p;//palindrome Tree currently has how many nodes link next;//adjacency table, indicating the current node palindrome string at both ends to add one character to form the palindrome string int new_node (int x) {//memset (next[p],        0,sizeof (Next[p]));        cnt[p]=0;        Next.clear (P);        num[p]=0;        Len[p]=x;    return p++;        } void Init () {next.clear ();        P=0;new_node (0);        New_node (-1);        last=0;        n=0;        S[0]=-1;    Fail[0]=1;        } int Get_fail (int x) {while (S[n-len[x]-1]!=s[n]) x=fail[x];    return x;        } int Add (int x) {x-= ' a ';        S[++n]=x;        int Cur=get_fail (last); if (! (    Last=next.get (cur,x)) {int Now=new_node (len[cur]+2);            Fail[now]=next.get (Get_fail (Fail[cur]), x);            Next.insert (Cur,x,now);    num[now]=num[fail[now]]+1;        Last=now;        } cnt[last]++;    return Num[last];        } ll Allsum ()//Find out the number of all palindrome substrings {LL ret=0; for (int i=p-1;i>0;i--) {cnt[fail[i]]= (cnt[fail[i]]+cnt[i])%MoD        Ret= (Ret+cnt[i])%mod;    } return ret;    }}tree;int Main () {scanf ("%d", &n);    scanf ("%s", str);    Tree.init ();    sum[n]=0;    for (int i=n-1;i>=0;i--) sum[i]= (Sum[i+1]+tree.add (Str[i]))%mod;    Tree.init (); LL ans=0; LL res=0;    LL res2=0;        for (int i=0;i<=n-1;i++) {ans= (ans+ (LL) Tree.add (Str[i]) *sum[i+1])%mod; Res2+=tree.num[tree.last];//can also ask for the number of all palindrome substrings}res=tree. Allsum ();//cout<<res<< "" <<res2<<endl; two are equal ans= ((LL) res* (res-1)/2%mod-ans)%    MoD    printf ("%lld\n", ans); return 0;}



Codeforces 17E palisection (palindrome tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.