天天看点

HDU 5658 CA Loves Palindromic (回文树)

CA Loves Palindromic

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 374 Accepted Submission(s): 161

Problem Description

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] are “a”,”b”.

In second query, the palindromic substrings in the substring 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.

Source

BestCoder Round #78 (div.2)

Recommend

wange2014 | We have carefully selected several similar problems for you: 5910 5909 5908 5907 5906

回文树。给出的串总长度只有1000,问任意区间中本质不同的回文串数量。因为长度才1000就暴力枚举区间即可。

#include "cstring"
#include "cstdio"
#include "string.h"
#include "iostream"
using namespace std;
const int MAXN =  ;
const int N =  ;
char str[];
struct Palindromic_Tree {
    int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
    int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
    int cnt[MAXN] ;
    int num[MAXN] ;
    int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
    int S[MAXN] ;//存放添加的字符
    int last ;//指向上一个字符所在的节点,方便下一次add
    int n ;//字符数组指针
    int p ;//节点指针

    int newnode ( int l ) {//新建节点
        for ( int i =  ; i < N ; ++ i ) next[p][i] =  ;
        cnt[p] =  ;
        num[p] =  ;
        len[p] = l ;
        return p ++ ;
    }

    void init () {//初始化
        p =  ;
        newnode (   ) ;
        newnode ( - ) ;
        last =  ;
        n =  ;
        S[n] = - ;//开头放一个字符集中没有的字符,减少特判
        fail[] =  ;
    }

    int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的
        while ( S[n - len[x] - ] != 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] +  ) ;//新建节点
            fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
            next[cur][c] = now ;
            num[now] = num[fail[now]] +  ;
        }
        last = next[cur][c] ;
        cnt[last] ++ ;
    }

    void count () {
        for ( int i = p -  ; i >=  ; -- i ) cnt[fail[i]] += cnt[i] ;
        //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
    }
}tree;

int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        long long ans[][];
        //Palindromic_Tree tree;
        scanf("%s",str+);
        tree.init();
        int len=strlen(str+);
        for(int i=;i<=len;i++)
        {
            tree.init();
            for(int j=i;j<=len;j++)
            {
                tree.add(str[j]);
                ans[i][j]=tree.p-;
            }
        }
        int cnt;
        scanf("%d",&cnt);
        while(cnt--)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            printf("%lld\n",ans[l][r]);
        }

    }
}