kmp刷题记录

 

kmp刷题记录

题目描述

给定一个字符串 A 和一个字符串 B,求 B 在 A 中的出现次数。

A 中不同位置出现的 B 可重叠,详见样例二、四。

输入格式

输入共两行,分别是字符串 A 和字符串 B。

输出格式

输出一个整数,表示 B 在 A 中的出现次数。

样例数据

input

RachelAhhhh
h

output

5

input

RachelAhhhh
hh

output

3

input

RachelAhhhh
hhh

output

2

input

zyzyzyz
zyz

output

3

题解

模板题

#include<bits/stdc++.h>
using namespace std;
char a[1000090],b[1000090];
int Next[1000090],cnt,len1,len2;
void GetNext()
{
	int i=2,j=0;
	Next[1]=0;
	for(;i<=len2;i++)
	{
		while(j>0&&b[i]!=b[j+1]) j=Next[j];
		if(b[i]==b[j+1]) j++;
		Next[i]=j; 
	}	
}
void KMP()
{
	for(int i=1,j=0;i<=len1;i++)
	{
		while(j>0&&a[i]!=b[j+1]) j=Next[j];
		if(a[i]==b[j+1]) j++;
		if(j==len2)
		{
			cnt++;
			j=Next[j];
		}
	}
}
int main()
{
	scanf("%s%s",a+1,b+1);
	len1=strlen(a+1);
	len2=strlen(b+1);
	GetNext();
	KMP();
	cout<<cnt;
	return 0;
}

[Usaco2015 Feb]Censoring

题目描述

Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material to read while waiting around in the barn during milking sessions. Unfortunately, the latest issue contains a rather inappropriate article on how to cook the perfect steak, which FJ would rather his cows not see (clearly, the magazine is in need of better editorial oversight).

FJ has taken all of the text from the magazine to create the string S of length at most 10^6 characters. From this, he would like to remove occurrences of a substring T to censor the inappropriate content. To do this, Farmer John finds the _first_ occurrence of T in S and deletes it. He then repeats the process again, deleting the first occurrence of T again, continuing until there are no more occurrences of T in S. Note that the deletion of one occurrence might create a new occurrence of T that didn't exist before.

Please help FJ determine the final contents of S after censoring is complete

有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程。

输入格式

The first line will contain S. The second line will contain T. The length of T will be at most that of S, and all characters of S and T will be lower-case alphabet characters (in the range a..z).

输出格式

The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.

样例数据

input

whatthemomooofun
moo

output

whatthefun

题解

      开一个栈,储存扫描到的每一个字符,当匹配成功时,把这个字串弹出,然后让指针j变回扫到当前栈顶时的j(可以开一个数组P记录一下扫到每一个位置时的j),继续往后匹配,再找到了就弹出,再让j=P[top]

      每找到一个,答案数加一

#include<bits/stdc++.h>
using namespace std;
char a[1000090],b[1000090];
int Next[1000090],cnt,len1,len2;
void GetNext()
{
	int i=2,j=0;
	Next[1]=0;
	for(;i<=len2;i++)
	{
		while(j>0&&b[i]!=b[j+1]) j=Next[j];
		if(b[i]==b[j+1]) j++;
		Next[i]=j; 
	}	
}
char ans[1000090];
int p[1000090];
int top=0;
void KMP()
{
	for(int i=1,j=0;i<=len1;i++)
	{
		ans[++top]=a[i];
		while(j>0&&a[i]!=b[j+1]) j=Next[j];
		if(a[i]==b[j+1]) j++;
		p[top]=j;
		if(j==len2)
		{
			j=p[top-len2];
			top-=(len2);	
		}
		
	}
}
int main()
{
	scanf("%s%s",a+1,b+1);
	len1=strlen(a+1);
	len2=strlen(b+1);
	GetNext();
	KMP();
	for(int i=1;i<=top;i++)
	putchar(ans[i]);
	return 0;
}

 

51nod 1277 字符串最大值

题目描述

一个字符串的前缀是指包含该字符第一个字母的连续子串,例如:abcd的所有前缀为a, ab, abc, abcd。 给出一个字符串S,求其所有前缀中,字符长度与出现次数的乘积的最大值。

例如:S = "abababa" 所有的前缀如下:

"a", 长度与出现次数的乘积 1 * 4 = 4,

"ab",长度与出现次数的乘积 2 * 3 = 6,

"aba", 长度与出现次数的乘积 3 * 3 = 9,

"abab", 长度与出现次数的乘积 4 * 2 = 8,

"ababa", 长度与出现次数的乘积 5 * 2 = 10,

"ababab", 长度与出现次数的乘积 6 * 1 = 6,

"abababa", 长度与出现次数的乘积 7 * 1 = 7. 其中"ababa"出现了2次,二者的乘积为10,是所有前缀中最大的

输入格式

输入字符串T, (1 <= L <= 1000000, L为T的长度),T中的所有字符均为小写英文字母。 (注意:原题是L <= 10W,这里加强一下!)

输出格式

输出所有前缀中字符长度与出现次数的乘积的最大值。

样例数据

input

abababa

output

10

题解

       先求出这个字符串自己的next数组, 当扫到某一个位置时,当前i的前缀实际上出现次数加了一(前提是next[i]!=0),因为next表示最长前缀和后缀,所以这个前缀在后缀中也出现了一次

      我们开一个数组记录每一个前缀的出现次数,然后倒着扫描,避免重复

      然后扫描时先把,num[i]+=1(它自己出现了一次了),在把num[next[i]]加上num[i],以next[i]为长度的前缀每被它后面的i访问时,都会作为一次后缀出现,所以要加上num[i];

#include<bits/stdc++.h>
using namespace std;
int next[1000070],n;
char s[1000600];
int cnt[1000600];
void Getnext()
{
	for(int i=2,j=0;i<=n;i++)
	{
		while(j>0&&s[i]!=s[j+1]) j=next[j];
		if(s[i]==s[j+1]) j++;
		next[i]=j;
	}
}
int main()
{
	int t=0;
	scanf("%s",s+1);
	n=strlen(s+1);
	Getnext();
	for(int i=n;i>=1;i--)
	{
		cnt[i]+=1;
		cnt[next[i]]+=cnt[i];
	}
	long long ans=0;
	for(int i=1;i<=n;i++)
	ans=max(1ll*i*cnt[i],ans);
	printf("%lld\n",ans);
	return 0;
 } 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值