Harry and magic string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 233 Accepted Submission(s): 117
Problem Description
Harry got a string T, he wanted to know the number of T’s disjoint palindrome substring pairs. A string is considered to be palindrome if and only if it reads the same backward or forward. For two substrings of
T:x=T[a1…b1],y=T[a2…b2]
(where a1 is the beginning index of
x,b1
is the ending index of x.
a2,b2
as the same of y), if both x and y are palindromes and
b1<a2 or b2<a1
then we consider (x, y) to be a disjoint palindrome substring pair of T.
Input
There are several cases.
For each test case, there is a string T in the first line, which is composed by lowercase characters. The length of T is in the range of [1,100000].
For each test case, there is a string T in the first line, which is composed by lowercase characters. The length of T is in the range of [1,100000].
Output
For each test case, output one number in a line, indecates the answer.
Sample Input
aca aaaa
Sample Output
3 15HintFor the first test case there are 4 palindrome substrings of T. They are: S1=T[0,0] S2=T[0,2] S3=T[1,1] S4=T[2,2] And there are 3 disjoint palindrome substring pairs. They are: (S1,S3) (S1,S4) (S3,S4). So the answer is 3.
Source
题意:给一个串s,问s中有多少个不相交的回文字串对
思路:
帖一发官方题解:
先求出p[i],表示以i为回文中心的回文半径,manacher,sa,hash都可以。然后我们考虑以i为回文中心的时候,可以贡献出多少答案。我们先考虑长度为p[i]的那个回文子串,它能贡献的答案,就是末尾在i-p[i]之前的回文子串数,那么长度为p[i-1]的,也是同样的道理。所以我们需要求一个f[x],表示末尾不超过x的回文子串总共有多少个,把f[i-p[i]]到f[i-1]累加起来即为以i为中心的回文子串能贡献的答案。那我们先统计,以x为结尾的回文子串有多少个,设为g[x]。来看以j为中心的回文子串,它的回文半径是p[j],那么从j到j+p[j]-1的这一段,都会被贡献一个回文子串,也就是这一段的g[]会被贡献1,这里的处理方法很多,不细说。然后求一次前缀和,即可得到f[x]
可以用树状数组维护前缀和,前缀和保存的是i点前面的所有回文串(回文串的最后一个字符要在i点之前)
然后求解的时候对以每一个点为中心的回文串中把所有回文串的第一个字符之前的前缀和加起来即可
比如有个回文串,中心点是mid,最左边为l 那么就是前缀和[mid-1]-前缀和[l-1]
注意用树状数组更新的时候为了快速,要区间更新,然后单点求得以每个字符结尾的回文串有多少个即可
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 200050
char s[N];
long long c[N],n;
long long p[N],qian[N],a[N];
int lowbit(int x)
{
return x&-x;
}
void add(int x,int v)
{
for(int i=x; i<=n; i+=lowbit(i))
c[i]+=v;
}
int sum(int x)
{
int ans=0;
for(int i=x; i; i-=lowbit(i))
ans+=c[i];
return ans;
}
int main()
{
while(~scanf("%s",s))
{
memset(c,0,sizeof(c));
memset(p,0,sizeof(p));
memset(a,0,sizeof(a));
memset(qian,0,sizeof(qian));
int len=strlen(s);
n=len;
for(int i=len-1; i>=0; i--)
{
s[i*2+1]=s[i];
s[i*2]='#';
}
s[len*2]='#';
int id=0,mx=0;
for(int i=1; i<=2*n; i++)
{
if(id+p[id]>i) p[i]=min(p[2*id-i],id+p[id]-i);
else p[i]=1;
while(i-p[i]>=0&&i+p[i]<=2*len&&s[i+p[i]]==s[i-p[i]])
p[i]++;
if(id+p[id]<i+p[i]) id=i;
}
for(int i=1; i<=2*n; i++)
{
int r=i+p[i]-1;
if(i%2==0)
{
if(r>=i)
{
add(i/2+1,1);
add(r/2+1,-1);
}
}
else
{
add(i/2+1,1);
add(r/2+1,-1);
}
}
for(int i=1; i<=n; i++)
{
a[i]=a[i-1]+sum(i);
qian[i]=qian[i-1]+a[i];
}
long long ans=0;
for(int i=1; i<=2*n; i++)
{
int l=i-p[i]+1;
if(i&1)
{
if(l/2>0) ans+=qian[i/2]-qian[l/2-1];
else ans+=qian[i/2];
}
else
{
if(p[i]>1)
{
if(l/2>0) ans+=qian[i/2-1]-qian[l/2-1];
else ans+=qian[i/2-1];
}
}
}
printf("%lld\n",ans);
}
return 0;
}