写题解前,先放出来一个大神的关于KMP的博客https://blog.csdn.net/v_july_v/article/details/7041827
说真的,昨天学长说今天讲的KMP会有点难,但是跟着听也能理解。果不其然,我中间玩了会QQ,就不会了= = 花了一下午时间去理解,最终明白了三四,但对于非模板题,还是需要一遍又一遍的去更好的理解KMP。

A题 Oulipo

The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter ‘e’. He was a member of the Oulipo group. A quote from the book:
Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…
Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T’s is not unusual. And they never use spaces.
So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {‘A’, ‘B’, ‘C’, …, ‘Z’} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with the word W, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
  • One line with the text T, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with |W| ≤ |T| ≤ 1,000,000.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

Sample Input

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

Sample Output

1
3
0

题意: 就是给一个字符串,然后给个目标字符串,求这个目标在长的那个字符串里面出现了几次

思路: 本人理解,所谓KMP,就是在暴力匹配的基础上,将每次匹配失败后进行下一匹配的位置进行优化,暴力匹配是每次往右移动一位,但这样会浪费很多时间在匹配过的点上。而KMP中引入了一个next数组,通过目标字符串的相同前缀后缀求出来其next数组,失配时,模式串向右移动的位数为:失配字符所在位置 - 失配字符对应的next 值。

求next数组的模板
有时候也把k初始化为0,j为1

void getnext(int len)
{ int j, k; j = 0; k = -1; //初始化值为-1且下标从0开始next[0] = -1; while(j < len)
{ if(k == -1 || b[j] == b[k]) { j++; k++; next[j] = k; } else k = next[k]; } } 

KMP模板

int kmp(int len1, int len2)
{ int i, j; int cnt = 0; i = 0; j = 0; getnext(len2); //得到目标串的next数组 while(i < len1) { if(j == -1 || a[i] == b[j]) { i++; j++; } else j = next[j]; if(j == len2) cnt++; //记录目标串出现的次数 } return cnt;
} 

完整AC代码

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxx = 1e6+10;
char s1[maxx],s2[maxx];int nextt[maxx];void getnext(int len)
{int j,k;j=0;k=-1;nextt[0] = -1;while(j<len){if(k == -1 || s2[k] == s2[j]){++k;++j;nextt[j] = k;}else{k = nextt[k];}}
}int kmp(int len1,int len2)
{int i=0;int j=0;int ans=0;getnext(len2);while(i<len1){if(j == -1 || s1[i] == s2[j]){++i;++j;}else{j = nextt[j];}if(j==len2){ans++;}}return ans;
}int main()
{int n;scanf("%d",&n);//cin>>n;while(n--){scanf("%s%s",s2,s1);//cin>>s2>>s1;int len1 = strlen(s1);int len2 = strlen(s2);printf("%d\n",kmp(len1,len2));//cout<<kmp(len1,len2)<<endl;}return 0;
}

注:

  1. cin cout会超时,需要用到scanf和printf,但是c语言的输入输出是不能直接对string类进行操作,方法以后再去学,这里暂时先把string换成了char。

  2. 求char类型长度用strlen()
    求string类型长度用str.length()

  3. next可能会在vj上报错,改成nextt。

【C++】henuACM暑期培训Day11 KMP相关推荐

  1. 【C++】henuACM暑期培训Day16 树状数组

    先放两个博客慢慢看 https://www.cnblogs.com/xenny/p/9739600.html https://blog.csdn.net/bestsort/article/detail ...

  2. 暑假计算机培训心得体会,暑期培训心得体会模板集锦10篇

    暑期培训心得体会模板集锦10篇 我们得到了一些心得体会以后,可用写心得体会的方式将其记录下来,这样有利于我们不断提升自我.那么写心得体会要注意的内容有什么呢?下面是小编整理的暑期培训心得体会10篇,欢 ...

  3. 暑期培训计划之个人计划

    使用算法竞赛入门经典(刘汝佳编) 暑期培训计划之个人计划(7.22到8.13) 日期 周次         看书                                            ...

  4. 高校计算机专业教师年终总结,计算机教师个人总结【高校计算机教师暑期培训总结】...

    高校师资培养是提高教师素质的有效途径,也是提高教育质量的重要环节.今天小编给大家整理了高校计算机教师暑期培训总结,谢谢大家对小编的支持. 高校计算机教师暑期培训总结篇一 在我们迈入二十一世纪的今天,以 ...

  5. 小学教师计算机应用培训通讯稿,暑期培训通讯稿

    相约暑期 快乐成长 ---南苑小学教师暑期校本培训 (通讯员:柳江漫)为进一步适应教育教学改革与发展的需要,切实提高教师课堂教学能力和实施素质教育的水平,促进教师的专业成长,2014年8月26日,高新 ...

  6. 北大ACM暑期培训感想

    博客从7月17日就没更新了,18日开始参加了半个月的ACM培训,然后一直刷老师课上讲过的题目.智商捉急,现在剩下计算几何这章不想刷了. 收获很多,锻炼了思维,学了几个算法,增加了受虐能力,培养了良好的 ...

  7. lftp命令使用 操作系统(Windows、Linux) PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 - powered by phpwhy.com

    狼剑 2006-12-13 00:49 <script language=JavaScript src="http://www.phpwind.com/src/nc.php" ...

  8. OUC暑期培训(深度学习)——第六周学习记录:Vision Transformer amp; Swin Transformer

    第六周学习:Vision Transformer & Swin Transformer Part 1 视频学习及论文阅读 1.Vision Transformer 原文链接:https://a ...

  9. OUC暑期培训(深度学习)——第一周学习记录:深度学习和pytorch基础

    第一周学习:深度学习和pytorch基础 目录 第一周学习:深度学习和pytorch基础 Part 1:视频学习: 1. 绪论: 2. 深度学习概述: Part 2:代码练习: 1. pytorch基 ...

最新文章

  1. swift 1.2 升级
  2. 计算机职称考试题目做完会有提示么,取得计算机职称的考试心得
  3. xlrd.biffh.XLRDError: Excel xlsx file; not supported报错
  4. endnote x9中科大版_文献管理软件Endnote的一些使用经验
  5. Hibernate框架 简述
  6. 架构设计:文件服务的设计与实现
  7. python怎么处理数据集的缺失值_python 对数据集的缺失值补全方法 sklearn.preprocessing.Imputer...
  8. paip.提升开发效率--终极方法---组件化及其障碍
  9. 关于ESP8266WIFI模块的介绍
  10. matlab 数组写入文件名,Matlab将数组写入.txt文件
  11. python爬取京东商品信息_python爬虫:爬取京东商品信息
  12. 高德地图开放平台的使用
  13. 图床:使用新浪微博相册
  14. 如何查询本机的内网IP地址
  15. 计算机基础知识考试模拟试题,计算机基础知识选择题考试必备考试真题模拟题...
  16. dell电脑更新win11后黑屏但有鼠标(已解决)
  17. 问题-某个程序改了ICO图标后编译后还是显示老图标?
  18. 计算机初操作员培训大纲,计算机初级培训大纲.doc
  19. 讯飞输入法 语音识别功能 台式机设置
  20. cmd导入python模块_Python如何导入模块

热门文章

  1. 想了解机器学习?这 3 种算法你必须要知道(中英文对照)
  2. 楚留香服务器维护,【楚留香】4月20日维护公告
  3. 安居客无锡二手房数据获取
  4. 方舟服务器 mod文件夹,方舟mod文件夹应该放在哪 | 手游网游页游攻略大全
  5. springmvc 采用MultipartResolver进行文件上传
  6. HCIE面试题之交换机和路由器收到未知表项数据包如何处理
  7. 华为模拟器小型网络组建
  8. fcpx插件Stupid Raisins Sale Pop for Mac(37种促销标题模板)
  9. 局部异常因子算法-Local Outlier Factor(LOF)
  10. 《Swift4打造今日头条视频实战项目实战》最新