HDU3336 Count the string 题解 KMP算法
题目链接: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算法的更多相关文章
- hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu3336 Count the string 扩展KMP
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
- 【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 ...
- 北京师范大学第十五届ACM决赛-重现赛J Just A String (kmp算法延伸)
链接:https://ac.nowcoder.com/acm/contest/3/J 来源:牛客网 Just A String 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...
- 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) ...
- hdu_3336: Count the string(KMP dp)
题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...
- HDU3336 Count the string KMP 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...
- 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 ...
随机推荐
- 洛谷P1978 集合 [2017年6月计划 数论08]
P1978 集合 题目描述 集合是数学中的一个概念,用通俗的话来讲就是:一大堆数在一起就构成了集合.集合有如 下的特性: •无序性:任一个集合中,每个元素的地位都是相同的,元素之间是无序的. •互异性 ...
- vue+ElementUI项目中,上传控件为必填项,上传图片后清空提示信息
(ps:以下是我在项目中遇到得问题及解决方法,希望对你们有帮助.如果还有其他方法,可以留言,谢谢) 一个表单页面,使用element-ui中el-upload上传图片,此项为必填项,然后写了校验规则, ...
- git pull 总提示让输入merge 信息
在生产环境拉去代码的时候,总是出现了 .git/MERGE_MSG,很烦. 虽然每次可以通过输入 :q 命令,取消,然后完成拉取.但是这样就很影响效率.解决办法一: 欺骗自己法只要我没看见这个问题,这 ...
- SQL Server新增用户并控制访问权限设置。
新增用户: 一.进入数据库:[安全性]—>[登录名]—>[新建登录名] 二.在常规选项卡中.如图所示,创建登录名.注意设置默认的数据库. 三.在[用户映射]下设置该用户所能访问的数据库.并 ...
- TextBlock中显示文字和图片,且不会自动换行
原本TextBlock显示图片是很容易的,即TextBlock.Inlines.Add(UiElement element):这个方法即可, 但是,会出现如下效果: 我不想要这种效果,所以改了下代 ...
- dijkstra算法 模板
算法理解见: https://www.bilibili.com/video/av18586085/?p=83 模板: #define INF 1000000000 int N; int dist[10 ...
- docker.[6] 数据卷
docker.[6] 数据卷 操作指令: # docker run -v /data1:/data2 -i -t centos /bin/bash 参数说明: data1 : 这里指的是宿主机的目录( ...
- web框架起源
web框架 python三大主流web框架 django 大而全,自带的组件和功能极多, 缺点:写小项目时候会比较笨重(杀鸡用牛刀),大并发不行,3000撑死 flask 小而精 自带的组件和功能极少 ...
- IOS 第三方管理库管理 CocoaPods
CocoaPod集成Tips http://www.jianshu.com/p/dcde0668eee9 import导入类失败 http://www.360doc.com/content/15/03 ...
- Python学习之路1☞简介及入门代码
在学习之前,首先了解一下python的前世今生 一.python简介与发展: python 是一种面向对象的解释性计算机程序设计语言. python由荷兰人Guido van Rossum 于1989 ...