Oulipo

Problem 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 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

题意描述:

找出a串中包含多少个b串

程序代码:

#include<stdio.h>
#include<string.h>
void get_next();
int next[10010],m,n;
char a[1000010],b[10010];
int main()
{int T,i,j,count;scanf("%d",&T);while(T--){scanf("%s%s",b,a);n=strlen(a);m=strlen(b);get_next();i=0;j=0;count=0;while(i<n){if(j==0&&a[i]!=b[j])i++;else if(j>0&&a[i]!=b[j])j=next[j-1];else{i++;j++;}if(j==m){j=next[j-1];count++;}}printf("%d\n",count);}return 0;
}
void get_next()
{int i,j;i=1;j=0;next[0]=0;while(i<m){if(j==0&&b[i]!=b[j]){next[i]=0;i++;}else if(j>0&&b[i]!=b[j])j=next[j-1];else{next[i]=j+1;i++;j++;}}
}

HDU-1686Oulipo(kmp)相关推荐

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

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

  2. Oulipo(kmp)

    Oulipo(kmp) The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...

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

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

  4. hdu 2224(dp)

    hdu 2224 (1)题意: 从1找一条到n的路径(路径上的前一个节点的编号小于后一个节点的编号), 然后找一条从n到1的路径,(路径上的前一个节点的编号大于后一个节点的编号)要求这两条路径遍历所有 ...

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

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

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

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

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

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

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

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

  9. hdu 5055(坑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5055 Bob and math problem Time Limit: 2000/1000 MS (J ...

  10. 字符串的模式匹配(KMP)算法

    一.背景 给定一个主串(以 S 代替)和模式串(以 P 代替),要求找出 P 在 S 中出现的位置,此即串的模式匹配问题. Knuth-Morris-Pratt 算法(简称 KMP)是解决这一问题的常 ...

最新文章

  1. 专访|黄铁军:人工智能不会以人为中心,但不要为此失落
  2. Hybird混合开发APP初学体验
  3. 《四世同堂》金句摘抄(八)
  4. Android笔记-ERROR: Invalid revision: 3.17.20200511-g8787079
  5. c++ 转bcd码_8421BCD码转换为十进制
  6. python虚拟环境安装和配置_Python3安装虚拟环境virtualenvwrapper和配置
  7. python协同过滤调用包_简单的python协同过滤程序实例代码
  8. vue实现留言板的功能_基于vue和bootstrap实现简单留言板功能
  9. vmware中NAT模式下,虚拟机与主机能ping通 为什么虚拟机不能上网
  10. 安装brew_MacBook Pro安装Homebrew慢的问题解决方案
  11. android+省电播放器,真的能省电?五款Android省电应用实测
  12. Android 投屏集成记录
  13. NANK南卡降噪耳机和小米蓝牙耳机哪个好?谁更值得入手?
  14. jane street market prediction 冠军方案 奇巧淫技与topline链接整理(3/3)
  15. 双目三维重建_系列篇|结构光三维重建基本原理
  16. Python——保存图片到本地
  17. 强化学习初探 DQN+PyTorch+gym倒立摆登山车
  18. 用python实现生成验证码图片
  19. 库房计算机管理,库房管理
  20. Linux学习-21-yum命令(查询、安装、升级和卸载软件包)和软件组管理

热门文章

  1. 2010——满地遍是网页防篡改和WAF
  2. 在Silverlight中使用DynamicMethod(动态方法)
  3. Spring结合Quartz实现多任务定时调用(转载)
  4. oracle dba create view 失败 解决办法
  5. Comments on The Ph.D. grind------by Yishi
  6. iOS多任务:suspend VS terminate
  7. VC读取PE文件的OEP
  8. 时间字符串与时间戳批量转换
  9. 我是学渣,但是我零基础自学web前端成功了
  10. 为什么 CSS 这么难学?