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

题目: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;	
	}
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值