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 ...
随机推荐
- 洛谷P1855 榨取kkksc03 [2017年4月计划 动态规划 09]
P1855 榨取kkksc03 题目描述 洛谷的运营组决定,如果一名oier向他的教练推荐洛谷,并能够成功的使用(成功使用的定义是:该团队有20个或以上的成员,上传10道以上的私有题目,布置过一次作业 ...
- 利用 awk 将当前的链接按端口汇总倒排序
写了一行命令,利用 awk 将当前的链接按端口汇总倒排序 :) netstat -ano | awk /tcp.*:1[15].*:[1-5]/'{print $4}' | awk -F ':' ' ...
- 跟我一起学extjs5(02--建立project项目)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jfok/article/details/35569057 跟我一起学extjs5(02--建立pro ...
- Appium 常用的API函数
常用的API函数[转] http://blog.sina.com.cn/s/blog_68f262210102vzf9.html 获取信息类API (1)获取默认系统语言对应的Strings.xml文 ...
- PHP数据加密和解密
encrypt.php <?php /** * Passport 加密函数 * * @param string 等待加密的原字串 * @param string 私有密匙(用于解密和加密) * ...
- 会话技术之cookie(记录当前时间、浏览记录的记录和清除)
cookie 会话技术: 当用户打开浏览器的时候,访问不同的资源,直到用户将浏览器关闭,可以认为这是一次会话. 作用: 因为http协议是一个无状态的协议,它不会记录上一次访问的内容.用户在访问过程中 ...
- Leetcode707.Design Linked List设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- 创建一个User类
1.用户模型—User类 用户模型或者叫账户模型,为什么这么说看下面代码 using System; using System.ComponentModel.DataAnnotations; name ...
- 移动项目到centos中运行报错:failed to open stream: Permission denied
这是文件夹没有读写权限的错误: (注意:TP5.0权限给runtime文件夹就行了,官方文档在安装tp5的方法中有介绍到权限问题) 在需要赋予权限的文件夹的前一级输入: chmod -R 文件夹名字
- LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II
链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can y ...