题目链接: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. 模拟7题解 T1方程的解

    方程的解 [扩展欧几里德] 首先进行特判,两个小时基本想到了,除了a!=0,b==0,a*c<0这种情况 其次就是一般情况: 首先exgcd求出ax+by=GCD(a,b)的一组任意解 然后两边 ...

  2. 第十章—DOM(二)——Element类型

    Element类型用于表现HTML和XML,提供了对元素标签名,子节点和特效的访问.Element节点具有以下特征: 要访问元素的标签名,可以使用nodeName属性,也可以使用tagName属性.这 ...

  3. Hibernate:**not found while looking for property: id https://blog.csdn.net/weixin_43827144/article/details/88935334

    https://blog.csdn.net/weixin_43827144/article/details/88935334 在程序执行时可能会报找不到属性的错误:例如:class Student n ...

  4. 关于父组件通过v-on接收子组件多个参数的一点研究

    写组件的时候遇到一个需求,我需要在子组件向父组件传递信息 this.$emit('myEvent', 信息1, 信息2) 在父组件使用v-on来接收 <my-component @myEvent ...

  5. 如何在Liferay 7中用html显示页面

    liferay portlet默认的显示页面是view.jsp,虽然可以在jsp中用include标签包括html文件,但是如何直接通过修改配置文件让默认的显示页面为view.html呢? 1.用Li ...

  6. JQuery-- 实例:小米左右切图,淡入淡出,自动,小圆点触发轮播图

    示意图: demo <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  7. Subsets 集合子集 回溯

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  8. iOS Method Swizzling和分类的妙用AppDelegate轻量化处理

    http://www.cocoachina.com/ios/20151117/14167.html 简介 在iOS工程中,AppDelegate往往会有上千行,甚至几千行,这样就会给维护AppDele ...

  9. xml入门与解析

    xml入门与解析 1.xml基础知识 xml:可扩展的标签语言,标签自定义. 作用:存储数据.(配置文件) 书写规范: 1.区分大小写 2.应该有一个根标签 3.标签必须关闭 <xx>&l ...

  10. js赋值符号“=”的小例子

    var obj1={x:5}; var obj2=obj1; obj1.a=obj1={x:6}; console.log(obj1.a); console.log(obj2.a); 为什么obj1. ...