Oulipo(kmp)

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模板

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=1e6+100;
char s[N],t[N];//s主,t模式
int tt,sl,tl,ne[N];
void getnext()
{ne[0]=-1;//下标为0的字符前没有字符串,单独处理,赋为-1 int i=0,j=-1;while(i<tl){if(j==-1||t[i]==t[j]){i++;j++;//对应字符匹配,i,j同步向后移 ne[i]=j;//赋值 }else//不匹配 ,i不变,j回溯j=ne[j];}
}
int kmp()
{int i,j=0,sum=0;//j=0初始化在模式串的第一个位置 for(i=0;i<sl;i++)//遍历整个主串 {while(j&&s[i]!=t[j])//顺着不匹配的边走,直到能匹配,最坏的情况j=0 j=ne[j];if(s[i]==t[j])//匹配成功,下一个位置 j++;if(j==tl)//在主串中找到了模式串 {sum++;//总数+1 j=ne[j];//下一个位置 //若不可重叠,j=0;}}return sum;
}
int main()
{scanf("%d",&tt);while(tt--){memset(ne,0,sizeof(ne));scanf("%s",t);scanf("%s",s);sl=strlen(s);tl=strlen(t);getnext();printf("%d\n",kmp());}return 0;}

Oulipo(kmp)相关推荐

  1. hdu 1686 Oulipo(kmp)

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  2. JavaScript实现knuth-morris-pratt(KMP)算法(附完整源码)

    JavaScript实现knuth-morris-pratt(KMP)算法(附完整源码) knuthMorrisPratt.js完整源代码 knuthMorrisPratt.test.js完整源代码 ...

  3. 杭电ACM-LCY算法进阶培训班-专题训练(KMP)

    杭电ACM-LCY算法进阶培训班-专题训练(KMP) 杭电ACM-LCY算法进阶培训班-专题训练(KMP) 剪花布条 Problem Description Input Output Sample I ...

  4. Python:实现knuth morris pratt(KMP)算法(附完整源码)

    Python:实现knuth morris pratt(KMP)算法 from __future__ import annotationsdef kmp(pattern: str, text: str ...

  5. Oulipo(KMP哈希)

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  6. POJ 3461 Oulipo(kmp算法解析)

    题目链接:https://cn.vjudge.net/contest/320014#problem/F Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI AVERD ...

  7. 第四章:2.串 -- 串的模式匹配算法(KMP)

    前言: 目录: 1.串类型的定义 2.串的表示和实现 3.串的模式匹配算法 4.串操作应用举例 正文: 串的模式匹配即,在给定主串S 中,搜索子串T 的位置,如果存在T 则返回其所在位置,否则返回 0 ...

  8. LeetCode 1392. 最长快乐前缀(KMP)

    1. 题目 「快乐前缀」是在原字符串中既是 非空 前缀也是后缀(不包括原字符串自身)的字符串. 给你一个字符串 s,请你返回它的 最长快乐前缀. 如果不存在满足题意的前缀,则返回一个空字符串. 示例 ...

  9. 字符串匹配算法(KMP)

    文章目录 1. KMP由来 2. KMP算法基本原理 3. 代码 4. Leetcode 28. 实现 strStr() 1. KMP由来 上一节说的BM算法是最高效.最常用的字符串匹配算法. 最知名 ...

最新文章

  1. C# richtextbox 自动下拉到最后 方法 RichTextBox读取txt中文后出现乱码
  2. 2013年下半年信息系统项目管理师考试论文试卷
  3. java split 逗号_java截取之空字符丢失
  4. HandlerInterceptor拦截器使用总结
  5. 开发者都应该知道的15个API
  6. 红豆、绿豆、黑豆、花生、莲子、薏仁米放在一起吃,可以吗?
  7. WDS部署服务所用的PXE引导文件
  8. 有关centos7 图形化root用户登录
  9. Hbase0.96 MVCC Lock 知识梳理
  10. python if 比较小数浮点数
  11. Vue + Echarts 实现中国地图的绘制
  12. 服务器系统安装蓝牙驱动,win10蓝牙驱动怎么安装?-win10蓝牙驱动的安装教程 - 河东软件园...
  13. 一款,整合百度翻译api跟有道翻译api的翻译君
  14. 华为云UGO正式亮相DTCC 2021,去“O”从此再无后顾之忧
  15. 批处理大全Win10版工具箱
  16. mysql如何创建用户代码_mysql 创建用户 并 受权_mysql
  17. 电子发票税费计算问题
  18. kubeadm High availability cluster
  19. 【wifi】抓取握手包
  20. 18.3.7给小可爱们的(NBUOJ)

热门文章

  1. mysql replication 监控_MySQL之-Replication监控及自动故障切换的详细分析
  2. word2vec的代码注释
  3. Codeforces 513D2 Constrained Tree
  4. c语言编写程序,输入正整数n,计算0到n以内所有奇数的和,c语言输入正整数n,计算1~n中的奇数和以及偶数和并输出....
  5. 一个比较全的C++农历算法
  6. 三层交换机原理及配置
  7. Linux操作系统安装及服务控制
  8. 修复损坏的gzip压缩文件方法之实用篇
  9. 在ABAQUS中使用多孔介质模型
  10. 丰田将在所有销售店安装充电设备丰田章男社长“建立共享基础设施”