[kmp+dp] hdu 4628 Pieces
来源:互联网 发布:淘宝客户端下载 编辑:程序博客网 时间:2024/05/21 12:42
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4622
Reincarnation
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 2096 Accepted Submission(s): 715
Problem Description
Now you are back,and have a task to do:
Given you a string s consist of lower-case English letters only,denote f(s) as the number of distinct sub-string of s.
And you have some query,each time you should calculate f(s[l...r]), s[l...r] means the sub-string of s start from l end at r.
Given you a string s consist of lower-case English letters only,denote f(s) as the number of distinct sub-string of s.
And you have some query,each time you should calculate f(s[l...r]), s[l...r] means the sub-string of s start from l end at r.
Input
The first line contains integer T(1<=T<=5), denote the number of the test cases.
For each test cases,the first line contains a string s(1 <= length of s <= 2000).
Denote the length of s by n.
The second line contains an integer Q(1 <= Q <= 10000),denote the number of queries.
Then Q lines follows,each lines contains two integer l, r(1 <= l <= r <= n), denote a query.
For each test cases,the first line contains a string s(1 <= length of s <= 2000).
Denote the length of s by n.
The second line contains an integer Q(1 <= Q <= 10000),denote the number of queries.
Then Q lines follows,each lines contains two integer l, r(1 <= l <= r <= n), denote a query.
Output
For each test cases,for each query,print the answer in one line.
Sample Input
2bbaba53 42 22 52 41 4baaba53 33 41 43 55 5
Sample Output
3175813851HintI won't do anything against hash because I am nice.Of course this problem has a solution that don't rely on hash.
Source
2013 Multi-University Training Contest 3
Recommend
zhuyuanchen520 | We have carefully selected several similar problems for you: 5061 5060 5059 5058 5057
给一个字符串,对每个字符串,有很多询问,询问给定区间不同子串的个数。
解题思路:
kmp+dp
普通的dp转移肯定超时。
sa[i][j]:表示以第j个字符开始能够往前最多的字符个数(假设为s个),要求满足【j-s+1,j】在【i,j-1】字符串区间出现。
这样要统计j开始的往前的情况,可以把字符串倒过来,把j作为第一个,然后1作为最后一个,求一遍next.然后更新sa[i][j] (i<j)
求出sa[i][j]后,就可以直接转移dp[i][j]=dp[i][j-1]+i-j+1-sa[i][j] //把第j个字符加上后,对整个子串个数的影响。减去在前面已经出现的。
代码:
#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>using namespace std;#define Maxn 2200char sa1[Maxn];char sa2[Maxn];int next[Maxn];int n,nn;int sa[Maxn][Maxn],dp[Maxn][Maxn];void getnext(){ int j=0; next[1]=0; for(int i=2;i<=nn;i++) { while(j>0&&sa1[j+1]-sa1[i]) j=next[j]; if(sa1[j+1]==sa1[i]) j++; next[i]=j; } return ;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%s",sa1+1); n=strlen(sa1+1); for(int i=1;i<=n;i++) sa2[i]=sa1[n+1-i]; for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) sa1[j-i+1]=sa2[j]; nn=n-i+1; getnext(); sa[n+1-i-1][n+1-i]=next[2]; for(int j=i+2;j<=n;j++) sa[n+1-i-(j-i)][n+1-i]=max(next[j-i+1],sa[n+1-i-(j-i)+1][n+1-i]); } for(int i=1;i<=n;i++) { dp[i][i]=1; for(int j=i+1;j<=n;j++) dp[i][j]=dp[i][j-1]+(j-i+1)-sa[i][j]; } int q; scanf("%d",&q); while(q--) { int a,b; scanf("%d%d",&a,&b); printf("%d\n",dp[a][b]); } } return 0;}
0 0
- [kmp+dp] hdu 4628 Pieces
- hdu 4628 - Pieces(压缩dp)
- hdu 4628 Pieces 集合DP
- [状压dp] hdu 4628 Pieces
- HDU-4628 Pieces 状压DP
- HDU 4628 Pieces (状压DP)
- hdu - 4628 - Pieces(状态压缩dp)
- HDU 4628 Pieces(状态压缩dp)
- hdu 4628 Pieces (状态压缩dp)
- hdu 4628 Pieces(状态压缩DP)
- hdu 4628 Pieces 状态压缩DP
- HDU 4628 Pieces(状态压缩DP)
- hdu 4628 Pieces(状压dp)
- hdu 4628 Pieces (状态压缩dp)
- hdu 4628 Pieces (状态压缩+二进制枚举+dp)
- HDU 4628 Pieces
- HDU 4628 Pieces
- Hdu 4628 Pieces
- 撞纵拽准浊抓左注组总撰踪总准祖转赚浊
- MySQL性能优化的最佳20+条经验
- 回环设备(loop-back devices)
- 安装qt-embedded-linux-opensource-src-4.5.3
- ClipMenu - Mac os 下的开发利器 多剪切板
- [kmp+dp] hdu 4628 Pieces
- second(圆柱体表面积)
- 左做驻邹邹祖酌着淄邹左赘奏罪拽嘴妆转
- MFC实现Edit输入限制(只允许输入数字,负号和小数点)
- yield:生成器
- 冒泡排序、快速排序和插入排序
- 单链表及其相关操作
- 独家记忆
- win7下本地访问远程Hadoop配置步骤