Palisection
time limit per test2 seconds
memory limit per test128 megabytes
inputstandard input
outputstandard 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 cross with 1..3
- 1..3 cross with 2..2
- 1..3 cross with 3..3
- 1..3 cross with 3..4
- 3..3 cross with 3..4
- 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
先处理出以某个位置为结尾的回文串数量,以及从某个位置到结束的所有回文串的数量。用回文串两两搭配的总数-所有拼接起来不是回文串的数量就是答案。
#include "cstring"
#include "cstdio"
#include "string.h"
#include "iostream"
#include "vector"
#define MOD 51123987
using namespace std;
const int MAXN = 2000005 ;
//const int N = 26 ;
char str[MAXN];
struct Palindromic_Tree {
vector<pair<int,int>> next[MAXN] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
//int cnt[MAXN] ;
int num[MAXN] ;
int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
int S[MAXN] ;//存放添加的字符
int last ;//指向上一个字符所在的节点,方便下一次add
int n ;//字符数组指针
int p ;//节点指针
int newnode ( int l ) {//新建节点
next[p].clear();
//cnt[p] = 0 ;
num[p] = 0 ;
len[p] = l ;
return p ++ ;
}
void init () {//初始化
p = 0 ;
next[0].clear();
next[1].clear();
newnode ( 0 ) ;
newnode ( -1 ) ;
last = 0 ;
n = 0 ;
S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
fail[0] = 1 ;
}
int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的
while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ;
return x ;
}
int find(int cur,int c)
{
for(int i=0;i<next[cur].size();i++)
{
if(next[cur][i].first==c)
return next[cur][i].second;
}
return 0;
}
int add ( int c ) {
c -= 'a' ;
S[++ n] = c ;
int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
int k=find(cur,c);
if ( k==0 ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
int now = newnode ( len[cur] + 2 ) ;//新建节点
fail[now] = find(get_fail ( fail[cur] ),c) ;//和AC自动机一样建立fail指针,以便失配后跳转
next[cur].push_back(make_pair(c,now));
num[now] = num[fail[now]] + 1 ;
}
last = find(cur,c) ;
//cnt[last] ++ ;
return num[last];
}
/*
void count () {
for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
}*/
} tree;
long long sum[MAXN],tail[MAXN];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(sum,0,sizeof(sum));
memset(tail,0,sizeof(tail));
scanf("%s",str+1);
tree.init();
for(int i=1;i<=n;i++)
{
tail[i]=tree.add(str[i]);
}
tree.init();
for(int i=n;i>=1;i--)
{
sum[i]=((sum[i+1]+tree.add(str[i]))%MOD)%MOD;
}
long long ans=sum[1]*(sum[1]-1)/2%MOD;
for(int i=2;i<=n;i++)
ans=(ans-tail[i-1]*sum[i]%MOD)%MOD;
ans=(ans+MOD)%MOD;
printf("%lld\n",ans);
}
return 0;
}