KMP CodeForces432D

6 篇文章 0 订阅

D. Prefixes and Suffixes

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character.

Let's introduce several definitions:

  • A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string sisi + 1...sj.
  • The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l].
  • The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|].

Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring.

Input

The single line contains a sequence of characters s1s2...s|s| (1 ≤ |s| ≤ 105) — string s. The string only consists of uppercase English letters.

Output

In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers li ci. Numbers li ci mean that the prefix of the length li matches the suffix of length li and occurs in string s as a substring ci times. Print pairs li ci in the order of increasing li.

Examples

input

Copy

ABACABA

output

Copy

3
1 4
3 2
7 1

input

Copy

AAA

output

Copy

3
1 3
2 2
3 1

 

做题目呢,一定要自己在草稿纸上写写画画演算一下。

题目大意:给一段字符串,找出前缀与后缀相同的长度,按照其长度从小到达,输出长度及在整个文本串中的出现次数。

通过在纸上胡乱写写画画的分析:Next[n]存储的是除了串本身,最长前缀与后缀相同的那个长度;而下一个能匹配的地方,就是Next[Next[i]]……一直匹配下去直到0,再加上串本身长度,就是所有的相同前缀后缀的长度答案了;kmp的Next数组可以说是很有点儿东西了。

统计出现次数方法:倒着从后往前,记录一个cnt数组,相当于动态规划了。一个子串的出现次数,是它本身长度在Next[]数组中的出现次数,再加上所有包含它的更长的长度,多的这部分如何去算?可以考虑到如果i后面某一位j,Next[j]=i,那么i前的串不是又出现了一次吗;并且这样不会重复计算~

最后输出即可~

 

#include <cstdio>
#include <cstring>

const int maxs=100020;

char str[maxs];
int Next[maxs];
int cnt[maxs],ans[maxs];
int len;

void get_Next(char* s,int* Next)
{
	Next[0]=Next[1]=0;
	for(int i=1;i<len;++i){
		int j=Next[i];
		while(j&&s[i]!=s[j]) j=Next[j];
		if(s[i]==s[j]) Next[i+1]=j+1;
		else Next[i+1]=0;
	}
}

int main()
{
	memset(cnt,0,sizeof(cnt));
	scanf("%s",str);
	len=strlen(str);
	get_Next(str,Next);
	for(int i=len;i;--i){
		cnt[i]+=1;
        if(Next[i]){
			cnt[Next[i]]+=cnt[i];
        }
	}
	int x=len,j=0;
	ans[0]=x;
	while(Next[x]){
		x=Next[x];
		ans[++j]=x;
	}
	printf("%d\n",j+1);
	for(;j>=0;--j){
		printf("%d %d\n",ans[j],cnt[ans[j]]);
	}

	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
KMP算法是一种字符串匹配算法,用于在一个字符串中查找另一个较短的模式串的出现位置。它的实现基于两个关键概念:部分匹配表和最大匹配前缀后缀数组(next数组)。通过构建next数组,KMP算法能够在失配时将模式串跳过一定长度,从而提高了匹配的效率。 在给定的引用内容中,有几个代码片段涉及到了使用Python实现KMP算法。这些代码分别是通过暴力搜索、利用next数组进行匹配以及计算next数组的实现。暴力搜索算法的时间复杂度为O(m*n),而KMP算法的时间复杂度为O(m+n),其中m为主串的长度,n为模式串的长度。 在KMP算法中,next数组记录了模式串中每个位置之前的最长公共前缀和最长公共后缀的长度。通过根据next数组的值来调整模式串的位置,KMP算法能够避免不必要的比较,从而提高匹配效率。 因此,Python中的KMP算法可以通过构建next数组来实现,在匹配过程中根据失配时的next值进行模式串的调整,直到找到匹配位置或匹配失败。这样可以在较短的时间内找到模式串在主串中的位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [KMP算法(python)](https://download.csdn.net/download/weixin_38618540/14854290)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [KMP算法(Python)](https://blog.csdn.net/m0_52238102/article/details/115830347)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值