HDU 5658 CA Loves palindromic (palindrome tree)

Source: Internet
Author: User

CA Loves palindromicTime limit:2000/1000 MS (java/others) Memory limit:262144/262144 K (java/others)
Total submission (s): 301 Accepted Submission (s): 131


Problem Descriptionca 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 is only counted once.
Inputfirst Line contains T Denoting the number of testcases.
T Testcases follow. For each testcase:
First line contains a string S . We ensure that it's contains only with the lower case letters.
Second Line contains a Interger Q , denoting the number of queries.
Then Q Lines follow, in each line there is intergers l,R , denoting the substring which is queried.
1≤T ≤Ten, 1≤LeNg Th≤ +, 1≤Q≤100000, 1≤L≤R≤LeNg Th
Outputfor each testcase, output of the answer in Q Lines.
Sample Input
1abba21 21 3

Sample Output
23 The number of different palindrome strings in the interval is 1000 we can use palindrome tree to find out the enumeration interval of different palindrome strings in each interval.
#include <iostream> #include <string.h> #include <algorithm> #include <stdlib.h> #include < Math.h> #include <stdio.h>using namespace std;typedef long long int ll;const int Max=100000;const int maxn=1000;c    Har str[maxn+5];int sum[maxn+5][maxn+5];struct tree{int next[max+5][26];    int num[max+5];    int cnt[max+5];    int fail[max+5];    int len[max+5];    int s[max+5];    int p;    int last;    int n;        int new_node (int x) {memset (next[p],0,sizeof (next[p]));        cnt[p]=0;        num[p]=0;        Len[p]=x;    return p++;        } void Init () {p=0;        New_node (0);        New_node (-1);        last=0;        n=0;        S[0]=-1;    Fail[0]=1;        } int Get_fail (int x) {while (S[n-len[x]-1]!=s[n]) x=fail[x];    return x;        } int Add (int x) {x-= ' a ';        S[++n]=x;        int Cur=get_fail (last); if (! (    Last=next[cur][x]) {int now=new_node (len[cur]+2);        Fail[now]=next[get_fail (Fail[cur])][x];            Next[cur][x]=now;            num[now]=num[fail[now]]+1;            Last=now;        return 1;        } cnt[last]++;    return 0;    } void Count () {for (int i=p-1;i>=0;p++) cnt[fail[i]]+=cnt[i];    }}tree;int Q;int Main () {int t;    scanf ("%d", &t);        while (t--) {scanf ("%s", str+1);        int Len=strlen (str+1);            for (int i=1;i<=len;i++) {tree.init ();               for (int j=i;j<=len;j++) {tree.add (str[j]);            Sum[i][j]=tree.p-2;        }} scanf ("%d", &q);        int l,r;            for (int i=1;i<=q;i++) {scanf ("%d%d", &l,&r);        printf ("%d\n", Sum[l][r]); }} return 0;}


HDU 5658 CA Loves palindromic (palindrome tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.