https://cn.vjudge.net/contest/314885#problem/C
CA loves strings, especially loves the palindrome strings.
One day he gets a string, he wants to know how many palindromic substrings in the substring S[l,r]S[l,r].
Attantion, each same palindromic substring can only be counted once.
Input
First line contains TT denoting the number of testcases.
TT testcases follow. For each testcase:
First line contains a string SS. We ensure that it is contains only with lower case letters.
Second line contains a interger QQ, denoting the number of queries.
Then QQ lines follow, In each line there are two intergers l,rl,r, denoting the substring which is queried.
1≤T≤10, 1≤length≤1000, 1≤Q≤100000, 1≤l≤r≤length1≤T≤10, 1≤length≤1000, 1≤Q≤100000, 1≤l≤r≤length
Output
For each testcase, output the answer in QQ lines.
Sample Input
1 abba 2 1 2 1 3
Sample Output
2 3
以每个字符为左区间端点建回文树,记录结果
回文树复杂度
设字符集大小为m,母串长度为n,则空间复杂度为O(nm),时间复杂度为O(nlogm)[也可以说是O(n),因为logm极小]
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3+10;
char s[N];
int ans[1005][1005];
struct Palindromic_Tree{
int next[N][26];//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
int fail[N];//fail指针,失配后跳转到fail指针指向的节点 最长回文后缀
int len[N];//len[i]表示节点i表示的回文串的长度
int S[N];//存放添加的字符
ll cnt[N];//结点表示的本质不同的回文串的个数(调用count()后)
int num[N];//结点表示的最长回文串的最右端点为回文串结尾的回文串个数
int last;//指向上一个字符所在的节点,方便下一次add
int n;//字符数组指针
int p;//节点指针
int newnode(int x){//新建节点
memset(next[p],0,sizeof(next[p]));
cnt[p]=0;
num[p]=0;
len[p]=x;
return p++;
}
void init(){
p=0;
newnode(0);
newnode(-1);
last=0;
n=0;
S[0]=-1;//开头放一个字符集中没有的字符,减少特判
fail[0]=1;
}
int get_fail(int x){//和KMP一样,失配后找一个尽量最长的
while(S[n-len[x]-1]!=S[n]) x=fail[x];
return x;
}
void add(int c){
c-='a';
S[++n]=c;
int cur=get_fail(last);//通过上一个回文串找这个回文串的匹配位置
if(!next[cur][c]){//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
int now=newnode(len[cur]+2);//新建节点
fail[now]=next[get_fail(fail[cur])][c];//和AC自动机一样建立fail指针,以便失配后跳转
num[now]=num[fail[now]]+1;
next[cur][c]=now;
}
last=next[cur][c];
cnt[last]++;
}
void count(){
for(int i=p-1;i>=0;i--) cnt[fail[i]]+=cnt[i];
//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
}
}pam;
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len;i++){
pam.init();
for(int j=i;j<len;j++){
pam.add(s[j]);
ans[i+1][j+1]=pam.p-2;
}
}
int Q;
scanf("%d",&Q);
while(Q--){
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",ans[l][r]);
}
}
return 0;
}