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
思路
若两个不同的匹配没有交集则j=0,若存在交集则j=next[j],
每次模式串匹配成功时,都将个数+1,然后从next数组的下一位继续开始匹配,直至待匹配串结束,(getnext,与匹配ab数组基本一样,需要注意的是getnext中是j=-1,匹配a,b是j=0)
代码

#include<stdio.h>
#include<string.h>
char s1[11234],s2[1001234];
int nextt[11234];
int cnt;
void getnext(char p[])
{int len=strlen(p);int i=0,j=-1;nextt[0]=-1;while(i<len){if(j==-1||p[j]==p[i]){j++;i++;nextt[i]=j;}elsej=nextt[j];}
}
int main()
{int n;scanf("%d",&n);while(n--){scanf("%s%s",s1,s2);int len1=strlen(s1);int len2=strlen(s2);getnext(s1);if(len1>len2){printf("0\n");    continue;}int i=0,j=0;cnt=0;while(i<len2){if(j==-1||s1[j]==s2[i]){i++;j++;}elsej=nextt[j];if(j==len1){j=nextt[j];cnt++;}}printf("%d\n",cnt);}return 0;
}

KMP(b中有几个a)相关推荐

  1. 字符串的模式匹配 (朴素模式匹配算法 ,KMP算法)

    字符串的模式匹配 寻找字符串p在字符串t中首次出现的起始位置 字符串的顺序存储 typedef struct {char str[MAXSIZE];int length; }seqstring; 朴素 ...

  2. KMP算法的next数组通俗解释

    我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见的改进算法,它可以在匹配过程中失配的情况下,有效地多往后面跳几个字符,加快匹配速度. 当然我们可以看到这个算法针对的是子串有对称属性, ...

  3. KMP算法 --- 深入理解next数组

    在KMP算法中有个数组,叫做前缀数组,也有的叫next数组. 每一个子串有一个固定的next数组,它记录着字符串匹配过程中失配情况下可以向前多跳几个字符. 当然它描述的也是子串的对称程度,程度越高,值 ...

  4. kmp有next和nextval的C语言,KMP模式匹配算法中next和nextval的求解(轉)

    KMP算法相關 轉載自:http://blog.sina.com.cn/s/blog_85b0ae450101j2iy.html KMP算法由兩部分組成: 第一部分,計算模式串的next或nextva ...

  5. 数据结构与算法之美笔记——基础篇(下):图、字符串匹配算法(BF 算法和 RK 算法、BM 算法和 KMP 算法 、Trie 树和 AC 自动机)

    图 如何存储微博.微信等社交网络中的好友关系?图.实际上,涉及图的算法有很多,也非常复杂,比如图的搜索.最短路径.最小生成树.二分图等等.我们今天聚焦在图存储这一方面,后面会分好几节来依次讲解图相关的 ...

  6. 【算法模板】轻松学会KMP算法

    目录 0.前言 1. 视频理解KMP的流程 2.代码实现 3.结束语 0.前言 KMP算法是在字符串中寻找字串的算法,时间复杂度为O(n). KMP算法中有两个关键因素: next数组 匹配机制 1. ...

  7. 转载大神的~~~~~~ KMP算法的前缀next数组最通俗的解释,如果看不懂我也没辙了

    KMP算法的前缀next数组最通俗的解释,如果看不懂我也没辙了 分类: 常用基础算法 IT经典笔试题 2012-09-02 10:09  1042人阅读  评论(1)  收藏  举报 算法 c 编程 ...

  8. KMP算法的前缀next数组最通俗的解释,如果看不懂我也没辙了

    我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见的改进算法,它可以在匹配过程中失配的情况下,有效地多往后面跳几个字符,加快匹配速度. 当然我们可以看到这个算法针对的是子串有对称属性, ...

  9. 【编程训练-考研上机模拟】综合模拟2-2019浙大上机模拟(晴神)

    A - next[i] Problem Description 在字符串匹配的KMP算法中有一个重要的概念是next数组,求解它的过程让不少同学伤透了心.next数组的直接语义其实是:使"长 ...

最新文章

  1. linux下的科学软件下载,十分科学app-十分科学官网版下载v1.4.3-Linux公社
  2. mysql联合查询sql优化
  3. 工业用微型计算机(15)-指令系统(10)
  4. hyperstudy联合matlab,HyperStudy对后处理排气管道参数的灵敏度分析及优化设计
  5. Android自己的自动化测试Monkeyrunner和用法示例
  6. QT5安装自己下载的mingw32及一些常见报错解决
  7. redis创建集群,redis-trib.rb命令详解
  8. Ubuntu桌面显示或隐藏回收站等图标
  9. NetLimiter Pro
  10. 微信小游戏关系链的使用(排行榜的显示)
  11. 晚清十大武林高手(电视里常引用的真实人物)
  12. Moses安装全记录
  13. hnu 模型机组合部件的实现(一)
  14. oracle any 语法,Oracle:apos;= ANY()apos;与apos;IN()apos; Dovov编程网
  15. 阿里云 DNS 新增云上线路的智能解析功能
  16. chen混沌系统的FPGA设计与实现
  17. c语言中符号是什么作用是什么,c语言中的符号|=是什么意思?
  18. nginx卸载与安装
  19. 【Java杂货铺】JVM#Java高墙之内存模型
  20. linux aux是什么命令,Linux命令ps aux详细解释

热门文章

  1. 常见积分和导数的推导
  2. PHP将Word转PDF文件
  3. 2022年12月各大学网络教育统考大学英语B考试题库及时间
  4. 基于微信小程序的校园互助平台
  5. 技嘉b365m小雕驱动工具_百元也有“雕”牌!技嘉B365M Aorus Elite主板评测
  6. 新一代智慧医疗整体解决方案
  7. iphone11卡住不能关机_iphone11怎么强制关机_苹果11promax的两种强制关机方法
  8. Python Matplotlib教程
  9. PHP项目开发案例全程实录pdf
  10. IDE报错 hdc_std version of the SDK does not match the hdcd version of the device.