OKR-Periods of Words(KMP)

Posted pureayu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OKR-Periods of Words(KMP)相关的知识,希望对你有一定的参考价值。

OKR-Periods of Words

  • 一道质量很高的KMP题目
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 10;
char s[N];
typedef long long ll;
ll ne[N];
int main(){
	int len;
	scanf("%d", &len);
	scanf("%s", s + 1);
	for(register int i = 2, j = 0; i <= len; i ++){
		while(j && s[i] != s[j + 1])
			j = ne[j];
		if(s[i] == s[j + 1])
			j ++;
		ne[i] = j;
	}
	ll ans = 0;
	for(register int i = 2; i <= len; i ++){
		if(ne[i] == 0)
			continue;
		else{
			int j = ne[i];
			while(ne[j])
				j = ne[j];
			if(ne[i])
				ne[i] = j; 
			ans += i - j; 
			
		}
	}
	cout << ans << endl;
	return 0;
}

以上是关于OKR-Periods of Words(KMP)的主要内容,如果未能解决你的问题,请参考以下文章

Luogu_P3435 [POI2006]OKR-Periods of Words KMP

Bzoj1511---OKR-Periods of Words(KMP)

[POI2006][luogu3435] OKR-Periods of Words [kmp+next数组]

bzoj1511[POI2006]OKR-Periods of Words KMP-next数组

P3435 [POI2006]OKR-Periods of Words

P3435 [POI2006]OKR-Periods of Words