[KMP算法]D. 【例题4】子串拆分
Code
#include <bits/stdc++.h>
#define N 15005
using namespace std;
int k, n, ans;
int t1[N];
char c[N], et[N];
int main ()
{
scanf ("%s%d", c + 1, &k);
n = strlen (c + 1);
for (int i = 1; i <= n; ++ i)
{
memset (t1, 0, sizeof (t1));
memset (et, 0, sizeof (et));
for (int j = 1; i + j - 1 <= n; ++ j)
et[j] = c[i + j - 1];
int tr = 0, het = strlen (et + 1);
for (int j = 2; j <= het; ++ j)
{
while (tr > 0 && et[tr + 1] != et[j]) tr = t1[tr];
if (et[tr + 1] == et[j]) tr ++;
t1[j] = tr;
}
tr = 0;
for (int j = 2; j <= het; ++ j)
{
while (tr > 0 && et[tr + 1] != et[j]) tr = t1[tr];
if (et[tr + 1] == et[j]) tr ++;
while (tr * 2 >= j) tr = t1[tr];
if (tr >= k) ans ++;
}
}
printf ("%d", ans);
return 0;
}
/*
aaaaa
1
*/