题目描述
You've got a string s=s_{1}s_{2}...\ s_{|s|}s=s1s2... s∣s∣ of length |s|∣s∣ , consisting of lowercase English letters. There also are qq queries, each query is described by two integers l_{i},r_{i}li,ri (1<=l_{i}<=r_{i}<=|s|)(1<=li<=ri<=∣s∣) . The answer to the query is the number of substrings of string s[l_{i}...\ r_{i}]s[li... ri] , which are palindromes.
String s[l...\ r]=s_{l}s_{l+1}...\ s_{r}s[l... r]=slsl+1... sr (1<=l<=r<=|s|)(1<=l<=r<=∣s∣) is a substring of string s=s_{1}s_{2}...\ s_{|s|}s=s1s2... s∣s∣ .
String tt is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t=t_{1}t_{2}...\ t_{|t|}=t_{|t|}t_{|t|-1}...\ t_{1}t=t1t2... t∣t∣=t∣t∣t∣t∣−1... t1 .
输入格式
The first line contains string ss (1<=|s|<=5000)(1<=∣s∣<=5000) . The second line contains a single integer qq (1<=q<=10^{6})(1<=q<=106) — the number of queries. Next qq lines contain the queries. The ii -th of these lines contains two space-separated integers l_{i},r_{i}li,ri (1<=l_{i}<=r_{i}<=|s|)(1<=li<=ri<=∣s∣) — the description of the ii -th query.
It is guaranteed that the given string consists only of lowercase English letters.
输出格式
Print qq integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces.
输入输出样例
输入 #1复制
caaaba 5 1 1 1 4 2 3 4 6 4 5
输出 #1复制
1 7 3 4 2
说明/提示
Consider the fourth query in the first test case. String s[4...\ 6]s[4... 6] = «aba». Its palindrome substrings are: «a», «b», «a», «aba».
链接:https://codeforces.com/problemset/problem/245/H
题意:q次询问,【l,r】区间内多少回文串。
题解:dp打表回文树,q次询问用O(1)查询。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e6+5;
int dp[5005][5005];//非模板
struct PAM{
int nex[maxn][30];//指向的串为当前串两端加上同一个字符构成
int fail[maxn];//fail跳转到自己这个串的最长回文后缀
int cnt[maxn];//出现次数
int num[maxn];// 表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数。
int len[maxn];//len[i]表示节点i表示的回文串的长度
int S[maxn];//存放添加的字符
int last,n,p;//last指向上一个字符所在的节点,方便下一次add
int create(int rt){//新建节点
memset(nex[p],0,sizeof(nex[p]));
cnt[p]=0;
num[p]=0;
len[p]=rt;
return p++;
}
void init(){//初始化
p=last=n=0;
create(0);
create(-1);
S[0]=-1;
fail[0]=1;
}
int getFail(int x){//寻找失败节点
while(S[n-len[x]-1]!=S[n]) x=fail[x];
return x;
}
void insert(int c)//插入字符
{
c=c-'a';
S[++n]=c;
int cur=getFail(last);
if(!nex[cur][c]){
int now=create(len[cur]+2);
fail[now]=nex[getFail(fail[cur])][c];
nex[cur][c]=now;
num[now]=num[fail[now]]+1;
}
last=nex[cur][c];
cnt[last]++;
}
void count()//cnt答案不准确,需要调用更新一下。
{
long long ans=0;
for (int i = p-1; i >= 0; i--)
cnt[ fail[i] ] += cnt[i];
}
void set(string s,int x){//设置字符串
init();
int len=s.size();
for(int i=0;i<len;i++)
{
insert(s[i]);
dp[x][x+i]=dp[x][x+i-1]+num[last];//非模板
}
}
}pam;
void solve(string s)//非模板
{
for(int i=1;i<s.length();i++)
{
pam.init();
string ss(s,i,s.length());
pam.set(ss,i);
}
}
int main()
{
string s;
cin>>s;
solve("#"+s);
int q;
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
cout<<dp[l][r]<<endl;
}
return 0;
}