[kmp+dp] hdu 4628 Pieces
题目链接:
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
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.
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.
2
bbaba
5
3 4
2 2
2 5
2 4
1 4
baaba
5
3 3
3 4
1 4
3 5
5 5
3
1
7
5
8
1
3
8
5
1HintI won't do anything against hash because I am nice.Of course this problem has a solution that don't rely on hash.
题目意思:
给一个字符串,对每一个字符串,有非常多询问,询问给定区间不同子串的个数。
解题思路:
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 2200 char 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;
}
[kmp+dp] hdu 4628 Pieces的更多相关文章
- HDU 4628 Pieces(DP + 状态压缩)
Pieces 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628 题目大意:给定一个字符串s,如果子序列中有回文,可以一步删除掉它,求把整个序列删除 ...
- hdu 4628 Pieces 状态压缩dp
Pieces Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total S ...
- hdu 4628 Pieces 状压dp
题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...
- HDU 4628 Pieces(状压DP)题解
题意:n个字母,每次可以删掉一组非连续回文,问你最少删几次 思路:把所有回文找出来,然后状压DP 代码: #include<set> #include<map> #includ ...
- hdu 4628 Pieces
http://acm.hdu.edu.cn/showproblem.php?pid=4628 状态压缩DP 时间复杂度应该是 16*(2^32) 但是运行时要远小于这个数 所以加一定剪枝就可以过 代码 ...
- HDU 4628 Pieces(状态压缩+记忆化搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=4628 题意:给个字符窜,每步都可以删除一个字符窜,问最少用多少步可以删除一个字符窜分析:状态压缩+记忆化搜索 ...
- hdu 4628 Pieces(状态压缩+记忆化搜索)
Pieces Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total S ...
- HDU 6153 A Secret ( KMP&&DP || 拓展KMP )
题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...
- [HDOJ5763]Another Meaning(KMP, DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...
随机推荐
- hbase开放lzo压缩
hbase仅仅支持对gzip的压缩,对lzo压缩支持不好. 在io成为系统瓶颈的情况下,一般开启lzo压缩会提高系统的吞吐量. 但这须要參考详细的应用场景,即是否值得进行压缩.压缩率是否足够等等. ...
- 得到JAVA项目根文件夹
获得的相对路径 说明:相对路径(这并不说明什么时候相对谁)可以通过以下来获得(无论是一般java项目或web工程) String path = System.getProperty("use ...
- Python学习笔记20:server先进
我们不依赖于一个框架,CGI如果是,只能使用socket介面.他完成了一个可以处理HTTP要求Pythonserver. 基于,不管是什么的计算机的操作系统(推荐Linux)和Python该计算机可被 ...
- POJ 1176 Party Lamps (DFS)
对于一束灯光.提供四种改变彩灯状态(ON<=>OFF)的操作:a.改变全部彩灯状态:b.改变奇数彩灯状态.c.改变偶数彩灯状态:d.改变3k+1号彩灯状态(1,4,7,10...). 给定 ...
- Zen Coding in Visual Studio 2012
http://www.johnpapa.net/zen-coding-in-visual-studio-2012 Zen Coding is a faster way to write HTML us ...
- 从苹果系统InstallESD.dmg里提取IOS
右键下载的Mac OS X Mountain Lion镜像:InstallESD.dmg,选择7-zip------打开压缩包 2.双击InstallMacOSX.pkg 3.选中InstallESD ...
- [原创].NET 业务框架开发实战之九 Mapping属性原理和验证规则的实现策略
原文:[原创].NET 业务框架开发实战之九 Mapping属性原理和验证规则的实现策略 .NET 业务框架开发实战之九 Mapping属性原理和验证规则的实现策略 前言:之前的讨论一直关注在怎么从D ...
- Matlab Newton‘s method
定义函数 function y=f(x) y=f(x).%函数f(x)的表达式 end function z=h(x) z=h(x).%函数h(x)的表达式 end 主程序 x=X;%迭代初值 i=0 ...
- HDU 1988 Cube Stacking (数据结构-并检查集合)
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 18834 Accepted: 6535 Ca ...
- (spring-第14回【IoC基础篇】)国际化信息 (转)
国际化又称为本地化. 当你把手机的language由中文切换到英文时,你的微信也相应改用英语,这就是i18n国际化.一般来说,应用软件提供一套不同语言的资源文件,放到特定目录中,应用根据不同语言的操作 ...