题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336

题目大意:找出字符串s中和s的前缀相同的所有子串的个数。

题目分析:KMP模板题。这道题考虑 nxt[] 数组的应用。以 s[i] 结尾的子串中一共有多少个子串可以作为s的前缀呢?我们只要令 t = nxt[i],cnt=0

每当 t!=-1,cnt++, t=nxt[t] 就可以了。

当然,我们可以用dp优化,dp[i] = dp[nxt[i]]+1 ,当然,如果 nxt[i]==-1 ,那么 dp[i] 就是 1,因为 s[0...i]

实现代码如下:

#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
#define MOD 10007
const int maxn = 1001000; int T, m, nxt[maxn], ans, f[maxn];
string t; void cal_next() {
m = t.length();
for (int i = 0, j = -1; i < m; i ++) {
while (j != -1 && t[j+1] != t[i]) j = nxt[j];
nxt[i] = (j+1 < i && t[j+1] == t[i]) ? ++j : -1;
}
} void solve() {
ans = 0;
for (int i = 0; i < m; i ++) {
if (nxt[i] == -1) f[i] = 1;
else f[i] = f[nxt[i]] + 1;
ans = (ans + f[i]) % MOD;
}
cout << ans << endl;
} int main() {
scanf("%d", &T);
while (T --) {
cin >> m >> t;
cal_next();
solve();
}
return 0;
}

作者:zifeiy

HDU3336 Count the string 题解 KMP算法的更多相关文章

  1. hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. hdu3336 Count the string 扩展KMP

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  3. HDU3336 Count the string —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others)     ...

  4. 【HDU 3336】Count the string(KMP+DP)

    Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...

  5. 北京师范大学第十五届ACM决赛-重现赛J Just A String (kmp算法延伸)

    链接:https://ac.nowcoder.com/acm/contest/3/J 来源:牛客网 Just A String 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...

  6. HDU 3336 Count the string(KMP的Next数组应用+DP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu_3336: Count the string(KMP dp)

    题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...

  8. HDU3336 Count the string KMP 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

随机推荐

  1. golang之select

    2.switch语句 (1) (2) 3.select语句 4.for语句 (1)常规式 (2)条件式 (3) (4) goto break continue fallthrought ------- ...

  2. python-null

    很早之前,遇到过一个面试官问的python中有没有null,当时有点紧张,想成了None,就脱口而出是有的.今天遇到了none问题,所以就正好说一下. python中是没有NULL的. Python中 ...

  3. C# System.Timers.Timer中的坑,程序异常退出后timer依然运行问题

    问题背景 C#小白,由于本公司IM系统服务端(java)是本人独立开发的,加上现在所在项目需要对接IM系统,于是IM的客户端(C#实现)对接工作就交给我了.于是C#小白的我天真的以为只要调用C#端的S ...

  4. tomcat配置证书

    [size=x-small][size=xx-large][size=medium] 1.利用java 生成一个.keystore文件 进入命令行(假设已经设定了环境变量) 执行 keytool -g ...

  5. input的表单验证(不断更新中~~)

    1 手机号验证 <input type="tel" id="phone" name="phone" placeholder=" ...

  6. Bnd教程(1):如何用命令行安装bnd

    1. 如果你用的是MacOS,请运行: brew install bnd 然后运行bnd version看是否安装成功: $ bnd version 4.0.0.201801191620-SNAPSH ...

  7. PHP核心编程-图像操作

    一 图像操作环境: 1.    开启GD2图像处理并检测 在php.ini开启GD库 2.    画布坐标系说明 二. 图像基本操作(步骤) 1.    创建图像 创建画布(图像资源) 创建的方法: ...

  8. R语言均值,中位数和模式

    R语言均值,中位数和模式 在R统计分析是通过用许多内置函数来执行的. 大多数这些函数是R基本包的一部分.这些函数需要R向量作为输入参数并给出结果. 我们正在讨论本章中的函数是平均数,中位数和模式. 平 ...

  9. Spring CommonsMultipartResolver上传文件小结

    自从业至今,文件上传与IO流之类的调用,一直是理解比较模糊的地方,大多就这网上搜到的资料抄抄改改草草了事,内部原理一直不甚了解,今日我们通过Spring的CommonsMultipartResolve ...

  10. 【md5加密】不可逆之简单例子原理

    import hashlib def md5_get(data): ret = hashlib.md5("gfdwuqmo@md1.".encode("utf-8&quo ...