【题目链接】
https://www.lydsy.com/JudgeOnline/problem.php?id=3620
【算法】
KMP
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXN 15010 int i,len,k,ans; char s[MAXN]; inline void kmp(int pos) { int i,j,tmp; static int nxt[MAXN]; nxt[pos] = 0; for (i = pos + 1; i <= len; i++) { tmp = nxt[i-1]; while (tmp && s[i] != s[pos+tmp]) tmp = nxt[pos+tmp-1]; if (s[i] == s[pos+tmp]) tmp++; nxt[i] = tmp; } j = 0; for (i = pos + 1; i <= len; i++) { while (j && s[i] != s[pos+j]) j = nxt[pos+j-1]; if (s[i] == s[pos+j]) j++; while (j > (i - pos) / 2) j = nxt[pos+j-1]; if (j >= k) ans++; } } int main() { scanf("%s%d",s+1,&k); len = strlen(s+1); for (i = 1; i <= len; i++) kmp(i); printf("%d ",ans); return 0; }