G - CA Loves Palindromic HDU - 5658

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].
Attantion, each same palindromic substring can only be counted once.
Input
First line contains T denoting the number of testcases.
T testcases follow. For each testcase:
First line contains a string S. We ensure that it is contains only with lower case letters.
Second line contains a interger Q, denoting the number of queries.
Then Q lines follow, In each line there are two intergers l,r, denoting the substring which is queried.
1≤T≤10, 1≤length≤1000, 1≤Q≤100000, 1≤l≤r≤length
Output
For each testcase, output the answer in Q lines.
Sample Input
1
abba
2
1 2
1 3
Sample Output
2
3

Hint
In first query, the palindromic substrings in the substring S [ 1 , 2 ] S[1,2] S[1,2] are “a”,“b”.
In second query, the palindromic substrings in the substring S [ 1 , 2 ] S[1,2] S[1,2] are “a”,“b”,“bb”.
Note that the substring “b” appears twice, but only be counted once.
You may need an input-output optimization.

题解:让你求l–r区间有多少个不同的回文子串。这是一道裸体。先预处理出来。
P:结点指针;
答案:P-2;

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=1e5+5;
struct link_list
{
  int u[maxn],v[maxn],ne[maxn],idx,h[maxn];
  void init()
  {
    memset(h,-1,sizeof(h));
    idx=0;
  }
  void init(int x)
  {
    h[x]=-1;
  }
  int get(int x,int y)
  {
    for(int i=h[x];i!=-1;i=ne[i])
    {
      if(u[i]==y) return v[i];
    }
    return 0;
  }
  void add(int x,int y,int c)
  {
    u[idx]=y;v[idx]=c;
    ne[idx]=h[x];
    h[x]=idx++;
  }
};
struct PAM
{
  int len[maxn],cnt[maxn];
  int fail[maxn],num[maxn];
  int S[maxn],ch[maxn][26];
  int last,p,n;
  //link_list nxt;
  int nowcode(int x)
  {
    for(int i=0;i<26;i++)  ch[p][i]=0;
   cnt[p]=0;num[p]=0;
    len[p]=x;
    return p++;
  }
  void init()
  {
    last=p=n=0;
    nowcode(0);
    nowcode(-1);
    S[0]=-1;
    fail[0]=1;
  }
  int get_fail(int x)
  {
    while(S[n-len[x]-1]!=S[n]) x=fail[x];
    return x;
  }
  void add(int c)
  {
    c=c-'a';
    S[++n]=c;
    int cur=get_fail(last);
    if(!ch[cur][c])
    {
      int now=nowcode(len[cur]+2);
      fail[now]=ch[get_fail(fail[cur])][c];
      ch[cur][c]=now;
      num[now]=num[fail[now]]+1;
    }
    last=ch[cur][c];
    cnt[last]++;
  }
}T;
int t,q,l,r;
int a[1005][1005];
char s[maxn];
int main()
{
  scanf("%d",&t);
  while(t--)
  {
    scanf("%s",s);
    int n=strlen(s);
    for(int i=0;i<n;i++)
    {
      T.init();
      for(int j=i;j<n;j++)
      {
        T.add(s[j]);
        a[i+1][j+1]=T.p-2;
      }
    }
    scanf("%d",&q);
    while(q--)
    {
      scanf("%d %d",&l,&r);
      printf("%d\n",a[l][r]);
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值