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

解题思路

kmp完全匹配。。。注意next[i]=j中保存的是:b[]中以第i个数(b[i-1])结尾的前j个数(包括b[i-1])匹配b[]的前j个数。

AC代码

第一种方法:简单但不太好理解

#include<stdio.h>
#include<string.h>
char s[10010],p[1000010];
int ne[10010];
void getnext(int m)
{int i=0,j=-1;ne[0]=-1;while(i<m){if(j==-1||s[i]==s[j]){i++;j++;ne[i]=j; }elsej=ne[j];}
//  for(int k=0;k<i;k++)
//  printf("%d",ne[k]);
}
int kmp(int m,int n)
{getnext(m);int i=0,j=0,sum=0;while(i<n){if(j==-1||p[i]==s[j]){i++;j++;   if(j==m){sum++;j=ne[j];}}elsej=ne[j];}return sum;
}
int main()
{int t;scanf("%d",&t);while(t--){
//      memset(ne,0,sizeof(ne));scanf("%s",s);getchar();scanf("%s",p);int m=strlen(s);int n=strlen(p);printf("%d\n",kmp(m,n));}return 0;
}

第二种方法:复杂但好理解

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
char a[10010],b[1000010];
int ne[10010];
int main()
{int t;scanf("%d",&t);while(t--){scanf("%s",a);getchar();scanf("%s",b);int alen=strlen(a);int blen=strlen(b);ne[0]=-1;for(int i=1,j=-1;i<alen;i++){while(j>=0&&a[i]!=a[j+1]) j=ne[j];if(a[i]==a[j+1])j++;ne[i]=j;} int sum=0;for(int i=0,j=-1;i<blen;i++){while(j!=-1&&b[i]!=a[j+1]) {j=ne[j];}if(b[i]==a[j+1]) j++;if(j==alen-1){sum++;j=ne[j];}}printf("%d\n",sum);}return 0;
}

F - Oulipo(kmp经典模板题)!!!相关推荐

  1. Simpsons’ Hidden Talents(辛普森一家的隐藏天赋 )(kmp经典模板题) HDU - 2594

    题目:Simpsons' Hidden Talents(辛普森一家的隐藏天赋 ) 中文大意 Homer: Marge, I just figured out a way to discover som ...

  2. B.背单词 。。 KMP。模板题

    大四了,Leyni感觉好惆怅,因为找不到工作,所以最后决定考研了,可是Leyni的英语好差,没办法,先从最基本的背单词开始吧.那么多单词怎么才好背呢,话说考研界盛传利用前缀背单词,貌似好神奇的样子.因 ...

  3. Knight Moves(骑士移动) (bfs经典模板题) POJ-2243

    题目:Knight Moves (骑士移动) 中文大意 A friend of you is doing research on the Traveling Knight Problem (TKP) ...

  4. KMP算法模板与解析

    全部数据结构.算法及应用课内模板请点击:https://blog.csdn.net/weixin_44077863/article/details/101691360 将KMP算法,就要先讲讲它是用来 ...

  5. Oulipo(欧力波)(经典kmp模板题) HDU-1686

    题目:Oulipo(欧力波) 中文大意 The French author Georges Perec (1936�C1982) once wrote a book, La disparition, ...

  6. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

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

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

  8. KMP 模板题 及next数组

    每天都在憧憬这些算法我都懂了的那一天...然鹅,KMP又看了一遍还是觉得稀里糊涂的.先放个模板题好了(:′⌒`) [hdu1711] Number Sequence Time Limit: 10000 ...

  9. BZOJ 2154 [国家集训队]Crash的数字表格 / JZPTAB(莫比乌斯反演,经典好题)(Luogu P1829)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 P1829 [国家集训队]Crash的数字表格 / JZPTAB(反演,经典好题) Problem S ...

最新文章

  1. PHP设计模式(4)命令链模式
  2. 错误处理: pip install 时候 “Read timed out”
  3. linux如何判断光盘是否挂载,LInux下如何挂载光盘找rpm包的方法步骤
  4. leetcode 1. 两数之和 思考分析
  5. 立潮头,冲击 “世界一流”——北大建设世界一流数学学科纪实
  6. oracle数据库自动备份脚本
  7. 3-34Pytorch与nn库
  8. JVM 内存区域方面的面试题
  9. python3字典列表_python3_列表、元组、集合、字典
  10. openstack连通性检查显示验证失败_从超大规模部署到一体机,浪潮云海引领OpenStack落地新范式...
  11. 线上展示3D可视化电子沙盘管理系统
  12. 抖音-相关分析和理解
  13. 欧拉角与方向余弦矩阵之间的转换
  14. 今天是冰桶算法大揭秘!!
  15. 二、stl ,模拟,贪心等 [Cloned] E - 贪心
  16. SQL server-数据库的创建
  17. 你这么努力,为何还如此焦虑?
  18. iso 国家名称列表
  19. Python爬虫教程:微医挂号网医生数据抓取
  20. 初创企业数据体系建设

热门文章

  1. H265封装成RTP流(一)
  2. 学计算机的能制造芯片,黑科技:木头造计算机芯片 还能降解
  3. Java爬虫Ins博主所有帖子的点赞和评论导出excel
  4. 让网站加载速度更快的10种方法
  5. 【转】交换机开发(一)—— 交换机的工作原理
  6. java为PDF添加水印,图片水印和文字水印
  7. 启动计算机引导win10,图文详解win10系统电脑开机引导错误的方案
  8. Java虚拟机参数配置
  9. AOP技术介绍--(.Net中关于AOP的实现)
  10. 华为——宏达电最需要害怕的新对手