详细KMP见http://blog.csdn.net/u014665013/article/details/37995355

Description

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 wordW and a textT, count the number of occurrences of W inT. All the consecutive characters of W must exactly match consecutive characters ofT. 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 stringW).
  • 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 wordW in the textT.

Sample Input

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

Sample Output

1
3
0

题意:

找出在目标串中有多少模式串(相邻两串中可以重叠)

代码

#include <stdio.h>
#include <string.h>
int next[10005];
char str1[1000005],str2[10005];
void getnext(int len)//此处也可以将int next[]改写成  int* next[]
{int j,k;j=0; k=-1; next[0]=-1;while(j<len){if(k==-1 ||str2[j]==str2[k]){j++;k++;next[j]=k;}else k=next[k];}
}
int kmp(int len1,int len2)//kmp算法
{int count=0;int i=0,j=0;getnext(len2);while(i<len1){if(j==-1||str1[i]==str2[j]){++i;++j;   //j控制子串回溯和遍历 }elsej=next[j];if(j == len2){count++;j = next[j];   //当有一个匹配成功的时候,相当于匹配失败,重新定位子串的匹配位置 }}return  count;
}
int main()
{int Case;int len1,len2;scanf("%d",&Case);getchar();//注意吸收空格 while(Case--){gets(str2);gets(str1);len1 = strlen(str1), len2 = strlen(str2);printf("%d\n",kmp(len1,len2));}return 0;
}

A - Oulipo(KMP算法经典)相关推荐

  1. Oulipo (KMP算法)

    Oulipo (KMP算法) 题目链接:HDU-1686 题目: Oulipo Problem Description The French author Georges Perec (1936–19 ...

  2. Oulipo(kmp算法)

    题目意思就是找子串在一个长串中出现的次数.  因为数据较大,一般的做法都超时,看了看kmp算法,用这个算法做的.这个算法最难的就是求那个next数组吧. #include<iostream> ...

  3. POJ3461 Oulipo ——KMP算法——Pku3461

    建议大家学一学比较巧妙的KMP算法吧,很有意思.推荐个题目:POJ3167 Cow Patterns 题解我会发在本博里. 这个KMP就木有什么好说的了吧,大家找百度百科学一下就可以了~ CODE P ...

  4. KMP算法经典题目--实现 strStr()

    28. 实现 strStr() :实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位 ...

  5. KMP算法经典应用——“循环节”

    题目链接:141. 周期 注意:此题字符串是由完整的循环节组成,即不存在末尾为循环节的一部分的情况 思路:找到最大与前缀相等的后缀, 举例: 蓝,绿为原字符串,都分割为5个部分,每部分为循环节 上方的 ...

  6. 经典算法题每日演练——第七题 KMP算法

    原文:经典算法题每日演练--第七题 KMP算法 在大学的时候,应该在数据结构里面都看过kmp算法吧,不知道有多少老师对该算法是一笔带过的,至少我们以前是的, 确实kmp算法还是有点饶人的,如果说红黑树 ...

  7. KMP算法: Oulipo

    这是一道北大OJ上的道 题目描述 The French author Georges Perec (1936–1982) once wrote a book, La disparition, with ...

  8. 记录KMP算法,记录其经典之处。。。

    离开学校已经多年了,早已经不再抚弄那些陈旧的书籍. 周末,深圳的天气阴沉,老天这段时间总是很乐意显摆,动不动就给深圳人民来次几十年一遇的暴雨,似乎要把一年的雨水全部在这些天下完似的. 所以呆在家里面看 ...

  9. 经典KMP算法C++与Java实现代码

    前言: KMP算法是一种字符串匹配算法,由Knuth,Morris和Pratt同时发现(简称KMP算法).KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的.比 ...

最新文章

  1. html h 不换行,css 强制不换行
  2. 联想 facebook android,Lenovo Vantage
  3. python 计时器 timeit 报错:ValueError: stmt is neither a string nor callable
  4. hadoop目录命令
  5. MySQL局域网内访问慢的原因及解决方法
  6. 月头月尾oracle取数,Oracle分析函数Over()的使用
  7. 联发科发布天玑1200芯片:6nm工艺 采用1+3+4三丛架构设计
  8. java spi机制_Java 双亲委派机制的破坏—SPI机制
  9. Spss的基本方法使用步骤
  10. ESP8266——入门:点亮TFT液晶屏(五)
  11. 中国最美的100首古代情诗
  12. java 如何将word 转换为ftl_使用FreeMarker导出word文档(支持导出图片)
  13. zabbix——告警媒介
  14. 你的平板,就是你的嵌入式开发板展示样机-----微核GUI对屌丝开发者的支持
  15. 【渝粤教育】电大中专跨境电子商务理论与实务 (24)作业 题库
  16. STM32+IAP方案的实现,IAP实现原理(详细解决说明)。
  17. ElasticSearch(ES)中的分片查询方式
  18. 笔记本频道-eNet笔记本大全-硅
  19. java parcelable_如何在Java代码中使用Parcelable
  20. #1024 程序员节 #各种由来

热门文章

  1. VMware虚拟机Linux增加磁盘空间的扩容操作
  2. ShardingSphere分库分表核心原理精讲第十一节 分布式事务详解
  3. 云米、品钛、趣店等中概股领跌,多家公司股价创历史新低
  4. 《OpenCV与ROS入门》讲座笔记
  5. vb读取文本文件某行的内容
  6. 进阶实验4-3.5 哈夫曼编码 (30 分)
  7. Kibana KQL查询语法
  8. Qt中使用QAxObject的dynamicCall和querySubObject函数操作SolidWorks的方法
  9. POJ1163 The Triangle
  10. NetTerm共享文件