HDU 5658 CA Loves Palindromic(回文树)
来源:互联网 发布:在职博士 知乎 编辑:程序博客网 时间:2024/06/05 19:39
CA Loves Palindromic
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 301 Accepted Submission(s): 131
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 substringS[l,r] .
Attantion, each same palindromic substring can only be counted once.
One day he gets a string, he wants to know how many palindromic substrings in the substring
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 stringS . We ensure that it is contains only with lower case letters.
Second line contains a intergerQ , denoting the number of queries.
ThenQ 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
First line contains a string
Second line contains a interger
Then
Output
For each testcase, output the answer in Q lines.
Sample Input
1abba21 21 3
Sample Output
23求区间内的本质不同的回文串的个数字符串的长度是1000我们可以利用回文树,求出每个区间内不同回文串的个数枚举区间#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;char 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;}
0 0
- HDU 5658 CA Loves Palindromic (回文树)
- HDU 5658 CA Loves Palindromic(回文树)
- HDU 5658 CA Loves Palindromic (回文树。)
- HDU 5658 CA Loves Palindromic
- HDU5658-CA Loves Palindromic
- HDU 5656 CA Loves GCD (dp)
- HDU-5655 CA Loves Stick(数学)
- HDU - CA Loves GCD
- CA Loves Stick HDU
- CA Loves Stick HDU
- BestCoder78 DIV1 T3 &&hdu5658 CA Loves Palindromic
- HDU 5655 CA Loves Stick
- HDU 5656:CA Loves GCD
- HDU 5655 CA Loves Stick
- HDU 5656 CA Loves GCD
- HDU 5656 CA Loves GCD
- HDU - 5655 CA Loves Stick
- hdu CA Loves GCD dp
- hdu 1247 Hat’s Words(字典树)
- 最小生成树的Prim算法
- 基本类型的类型转换
- Ubuntu Cannot run program "../SDK/build-tools/xxx/aapt": erro = 2 No such file or directory
- Startservice的生命周期
- HDU 5658 CA Loves Palindromic(回文树)
- Oracle 横表变纵表
- 安卓017ListView & GridView & ScrollView
- BZOJ4382: [POI2015]Podział naszyjnika
- Java WebService 简单实例
- wc
- MySql数据库之引擎
- 删除线
- jdbc template 学习总结