CodeForces 17E Palisection

Description

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 modulo51123987.

Sample Input

Input
4
babb
Output
6
Input
2
aa
Output

2

求有多少对回文子串相交,那么先求出不想交的,再拿总数减一下即可。

manacher算法版

#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int low(int x){ return x&-x; }
const int mod = 51123987;
const int maxn = 4e6 + 10;
int n, f[maxn], k[2][maxn];
char s[maxn], S[maxn];

void add(int x, int y, int z)
{
	for (int i = y; i <= n; i += low(i)) k[x][i] += z;
}

int get(int x, int y)
{
	int ans = 0;
	for (int i = y; i; i -= low(i)) ans += k[x][i];
	return ans;
}

void manacher(char *s)
{
	//字符串处理,增加分隔符
	S[n + n + 2] = 0; S[0] = 2;
	for (int i = 1; i <= n; i++) k[0][i] = k[1][i] = 0;
	for (int i = 0; s[i]; i++)
	{
		S[i + i + 3] = S[i + i + 1] = 1;
		S[i + i + 2] = s[i];
	}

	//manacher算法,计算出以每个字符为中心的最长回文串
	int mx = 0, id = 0, ans = 0;
	for (int i = 1; S[i]; i++)
	{
		if (mx > i) f[i] = min(f[id + id - i], mx - i);
		else f[i] = 1;
		while (S[i + f[i]] == S[i - f[i]]) f[i]++;
		if (f[i] + i > mx)
		{
			mx = f[i] + i;
			id = i;
		}
		if (i & 1)
		{
			if (f[i] < 2) continue;
			add(0, (i - 1) / 2 - f[i] / 2 + 1, 1);	add(0, (i + 1) / 2, -1);
			add(1, (i + 1) / 2, 1);	add(1, (i + 1) / 2 + f[i] / 2, -1);
		}
		else
		{
			add(0, i / 2 - f[i] / 2 + 1, 1);	add(0, i / 2 + 1, -1);
			add(1, i / 2, 1);	add(1, i / 2 + f[i] / 2, -1);
		}
	}
}

int main()
{
	while (scanf("%d%s",&n, s) != EOF)
	{
		manacher(s);
		LL L = 0, R = 0, ans = 0;
		for (int i = 1; i <= n; i++)
		{
			L = get(0, i);
			(ans += L * R %mod) %= mod;
			(R += get(1, i)) %= mod;
		}
		ans = (R*(R - 1) / 2 - ans) % mod;
		printf("%lld\n", ans);
	}
}

回文树版
#pragma comment(linker, "/STACK:102400000,102400000")
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 51123987;
const int maxn = 2e6 + 10;
int n;
LL a[maxn];
char s[maxn];

struct linklist
{
	int nt[maxn],ft[maxn],u[maxn],v[maxn],sz;
	void clear(){sz=0;}
	void clear(int x){ ft[x]=-1;}
	int get(int x,int y)
	{
		for (int i=ft[x];i!=-1;i=nt[i])
		{
			if (u[i]==y) return v[i];
		}
		return 0;
	}
	void insert(int x,int y,int z)
	{
		u[sz]=y;	v[sz]=z;
		nt[sz]=ft[x];	ft[x]=sz++;
	}
};

struct PalindromicTree
{
	const static int maxn = 2e6 + 10;
	const static int size = 26;
	linklist next;
	int sz, tot, last;
	int fail[maxn], len[maxn], cnt[maxn];
	char s[maxn];
	void clear(int x)
	{
		len[1] = -1; len[2] = 0;
		fail[1] = fail[2] = 1;
		cnt[1]=cnt[2]=tot=0;
		last = (sz = 3) - 1;
		next.clear();
		next.clear(1);	next.clear(2);
	}
	int Node(int length)
	{
		len[sz] = length; cnt[sz]=1; 
		next.clear(sz);
		return sz++;
	}
	int getfail(int x)
	{
		while (s[tot] != s[tot - len[x] - 1]) x = fail[x];
		return x;
	}
	int add(char pos)
	{
		int x = (s[++tot] = pos) - 'a', y = getfail(last);
		if (!(last=next.get(y,x)))
		{
			next.insert(y,x,last = Node(len[y] + 2));
			fail[last] = len[last] == 1 ? 2 : next.get(getfail(fail[y]),x);
			cnt[last]+=cnt[fail[last]];
		}
		return cnt[last];
	}
}solve;

int main()
{
	while (scanf("%d%s", &n, s) != EOF)
	{
		LL ans=a[0]=0,len=strlen(s);
		solve.clear(len);
		for (int i = 1; i<=len; i++) 
		{
			a[i]=(a[i-1]+solve.add(s[i-1]))%mod;
		}
		solve.clear(len);
		for (int i=len;i;i--)
		{
			(ans+=solve.add(s[i-1])*a[i-1]%mod)%=mod;
		}
		printf("%lld\n",(a[len]*(a[len]-1)/2-ans+mod)%mod);
	}
	return 0;
}

你可能感兴趣的:(codeforces)