题目:Oulipo(欧力波)

  • 中文大意

The French author Georges Perec (1936�C1982) 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

中文大意

法国作家乔治·佩雷克 (Georges Perec,1936-C1982) 曾写过一本书,《失踪》,没有字母“e”。他是Oulipo集团的成员。书中引述:

一切看起来都很正常,但一切都声称是错误的。一开始一切正常,然后就出现了非人、可怕的东西。他很想知道将他与小说联系起来的联系在哪里表达:在他的地毯上,任何时候都在冲击他的想象力,禁忌的直觉,隐晦的邪恶,空洞的事物的愿景。 :愿景,对控制一切的遗忘的预期,在那里理性被废除:一切似乎都很正常,但…

在接下来的比赛中,佩雷克可能会获得高分(或者更确切地说,低分)。人们被要求就某个主题写一篇甚至可能有意义的文本,尽可能少地出现给定的“单词”。我们的任务是为陪审团提供一个计算这些事件的程序,以便获得参赛者的排名。这些竞争者经常写很长的文字,意思是无意义的;500,000 个连续的“T”序列并不罕见。他们从不使用空格。

所以我们想快速找出一个词,即给定的字符串,在文本中出现的频率。更正式地:给定字母表 {‘A’, ‘B’, ‘C’, …, ‘Z’} 和该字母表上的两个有限字符串,一个单词 W 和一个文本 T,计算 W 在 T 中的出现次数. W 的所有连续字符必须与 T 的连续字符完全匹配。出现可能重叠。

输入
输入文件的第一行包含一个数字:要遵循的测试用例的数量。每个测试用例具有以下格式:

一行包含单词 W,一个字符串在 {‘A’, ‘B’, ‘C’, …, ‘Z’} 上,其中 1 ≤ |W| ≤ 10,000(此处 |W| 表示字符串 W 的长度)。
一行文本 T,一个字符串在 {‘A’, ‘B’, ‘C’, …, ‘Z’} 上,带有 |W| ≤ |T| ≤ 1,000,000。
输出
对于输入文件中的每个测试用例,输出应该在一行中包含一个数字:单词 W 在文本 T 中的出现次数。

样本输入
3
BAPC
BAPC
AZA
阿扎扎
给定
AVERDXIVYERDIAN
样本输出
1
3
0

解题思路: 本题需要你判断给出的两个字符串,第二串里面可以找出来几个第一串,经典的kmp模板题。

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
const int maxn = 1e6+5;
char str[maxn],ptr[maxn];
int Next[maxn];
int slen,plen;
//输出子串在模板串中出现了几次,可重叠
void gtnext()
{int k=-1;int j=0;while(j<plen){if(k==-1||ptr[j]==ptr[k]){++j;++k;if(ptr[j]!=ptr[k])//优化next,使kmp更加快一点 Next[j]=k;else Next[j]=Next[k];}else k=Next[k];}return ;
}int kmp()
{int cnt=0;int i=0,j=0;while(i<slen){if(j==-1||str[i]==ptr[j]){i++;j++;}else j=Next[j];if(j==plen){cnt++;j=Next[j];//完成一次匹配,将j移动到最长的前缀处,省略不必要的查找 }}return cnt;
}
int main()
{int n;cin>>n;while(n--){scanf("%s%s",ptr,str);//子串在上 slen=strlen(str);plen=strlen(ptr);Next[0]=-1;gtnext();cout<<kmp()<<endl;   }
}

Oulipo(欧力波)(经典kmp模板题) HDU-1686相关推荐

  1. POJ Oulipo(KMP模板题)

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

  2. POJ:3461-Oulipo(KMP模板题)

    原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...

  3. KMP 模板题 及next数组

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

  4. hihoCoder 1015 (KMP模板题)

    题目链接:http://hihocoder.com/problemset/problem/1015 Time Limit:1000ms Case Time Limit:1000ms Memory Li ...

  5. Codeforce-126B:Password(KMP模板题)

    题目链接:点击打开链接 题目大意: 给你一个串,让你求这个串的一个同时是前缀,后缀(这个说法好像不太对)且在串中出现过的最长子串. 举个例子: 对于串 fixprefixsdfix 就应该输出fix. ...

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

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

  7. 一个斐波那契数列题 HDU 2041

    有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? Input 输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1< ...

  8. 一道母函数的模板题 (hdu 2082)

    终于算弄明白母函数是什么东西了 = = , 呼~ 感觉它利用了高中学的二项式定理(就是用到组合数的那个) 完完全全的利用啊~~!以前用二项式求出最后那超长的x的表达式( 1 + C(n,1)*x^1 ...

  9. Simpsons’ Hidden Talents【KMP模板题】

    Homer: Marge, I just figured out a way to discover some of the talents we weren't aware we had.  Mar ...

最新文章

  1. 华为RH8100v3巡检
  2. HTTP 协议中的 cookie
  3. 售后服务成OA品牌竞争重要因素
  4. 2018年第九届蓝桥杯 - 省赛 - C/C++大学A组 - A. 分数
  5. 「2018山东一轮集训」Game
  6. 如何修改VC6的项目名
  7. 判断深度学习模型的稳定性_全自动搭建定制化深度学习模型
  8. 深入理解Scala的隐式转换系统
  9. 站在K2角度审视流程--任务的独占与释放
  10. loss 加权_【转载】keras 自定义 loss损失函数, sample在loss上的加权 和 metric
  11. 云原生持续交付的模式和实践
  12. 第三届《麻省理工科技评论》EmTech China峰会召开,全球新兴科技智慧风暴席卷京城
  13. eclipse查看git地址_40个适用于Linux管理员和开发人员的git命令(上)
  14. Atitit 跨平台跨语言图像处理与node.js图像处理之道 attilax著 1. 著名跨语言类库 ImageMagick简介、GraphicsMagick、命令行 1 1.1. opencv
  15. python exception最简单的应用(基本可以满足大部分需求)(异常、raise的使用)
  16. MPLAB X环境ICD3无法使用
  17. Quartz——CronTrigger触发器
  18. Android 系统字体
  19. 【愚公系列】2021年12月 攻防世界-简单题-CRYPTO-005(Railfence)
  20. 计算机网络学习记录——模块一 网络互联基础

热门文章

  1. JavaWeb完整笔记
  2. jacob 给word 指定位置添加超级链接
  3. 霸气女创始人刘楠:我挖了迪士尼高管,2年把公司干到100亿!
  4. Linux 中的 nl 命令详解及C/C++代码实现(文件行数)
  5. 用计算机制作动画,什么是计算机动画制作
  6. 常用的分类问题中的损失函数
  7. xray和burp联动
  8. android 换行符 编码_android中的常见的占位符及转义字符
  9. 一键实现Windows和MacOS同屏操作,是什么神级体验?
  10. swagger Could not resolve pointer: /definitions/Person does not exist in document