Title Description
After the idle boredom of $ YGH $ seconds off the above two questions, start thinking about interesting palindrome string problem.
Before he had a floating string. Apparently $ YGH $ will $ manacher $, so he readily obtained a number of palindromic substring of this string. However, the number palindromic substring in question he is not satisfied, he intends to come up with a data structure to quickly determine the subscript string $ [l, r] $ substrings (the same palindromic repeat substring count). But this is too simple matter, he intends to test to $ YYR $ spicy chicken, spicy chicken but extremely $ YYR $ absolutely no idea.
So, $ YGH $ sped away, in a small piece of dust brought up the sleeves, contemplative $ YYR $ still there.
Input Format
The first line of a string $ S $.
The second line an integer $ T $, represents the number of inquiries.
Then $ T $ lines of two integers $ l $, $ r $, $ query string indicates the subscript S $ $ [l, r] $ substring answers.
Output Format
$ T $ output lines, each represents an integer answer to this inquiry.
Sample
Sample input:
Ababaab
2
L 3
3 7
Sample output:
4
8
Data range and tips
For $ 20 \% $ of the data, to ensure that $ | S |, T \ leqslant 500 $
for the data $ 40 \% $ ensure $ | S |, T 5,000 $ \ leqslant
data for $ 100 \% $ ensure $ | S | \ leqslant 5,000, T \ leqslant 100,000 $
answer
I wish you all a happy National Day, Happy training!
The first question to be more abstract, the definition of a two-dimensional array $ Map $, if the interval $ [l, r] $ string is a palindrome, then $ Map [l] [r] = 1 $, otherwise $ 0 $.
So, all we need is the required point $ (l, l) $ the point $ (r, r) $ direct a few $ 1 $.
Palindrome sequence is not required in front of the hash $ $ process can achieve, the number of seek $ 1 $ prefix and can be used later.
Time complexity: $ \ Theta (n ^ 2 + T) $.
Expectations score: $ 100 $ points.
Actual score: $ 100 $ points.
Code time
#include<bits/stdc++.h>
using namespace std;
int n;
char ch[5001];
int S[5001];
int Map[5001][5001];
unsigned long long flag[5001];
unsigned long long hash1[5001],hash2[5001];
void pre_work()
{
flag[0]=1;
for(int i=1;i<=n;i++)
{
flag[i]=flag[i-1]*131;
hash1[i]=hash1[i-1]*131+S[i];
}
for(int i=n;i;i--)hash2[i]=hash2[i+1]*131+S[i];
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
if(hash1[j]-hash1[i-1]*flag[j-i+1]==hash2[i]-hash2[j+1]*flag[j-i+1])
Map[i][j]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
Map[i][j]+=Map[i-1][j]+Map[i][j-1]-Map[i-1][j-1];
}
int main()
{
scanf("%s",ch+1);
n=strlen(ch+1);
for(int i=1;i<=n;i++)
S[i]=ch[i]-'a'+1;
pre_work();
int T;scanf("%d",&T);
while(T--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",Map[r][r]-Map[l-1][r]-Map[r][l-1]+Map[l-1][l-1]);
}
return 0;
}
rp ++