Pagini recente » Cod sursa (job #720217) | Cod sursa (job #1057243) | Cod sursa (job #1955859) | Cod sursa (job #1586323) | Cod sursa (job #627931)
Cod sursa(job #627931)
#include<iostream>
#include<fstream>
using namespace std;
const int MAXN = 6010;
const int MOD = 66601323;
const int B = 29;
char s[MAXN];
int c[MAXN], d[MAXN];
int main() {
ifstream f("per.in");
ofstream g("per.out");
int n, k;
f >> n >> k;
f >> s;
int result = 0;
for (int l = 1; l <= n/k; l++) {
int strValue = 0;
int m = 1;
for (int i = 0; i < l; i++) {
strValue = (strValue*B + (s[i] - 'a')) % MOD;
m = (m*B) % MOD;
}
c[l-1] = strValue;
d[l-1] = 1;
for (int i = l; i < n; i++) {
strValue = (strValue*B + (s[i] - 'a')) % MOD;
strValue -= ((s[i-l] -'a')*m) % MOD;
if (strValue < 0) {
strValue += MOD;
}
c[i] = strValue;
d[i] = 1;
if (c[i] == c[i-l]) {
d[i] += d[i-l];
}
if (d[i] >= k) {
result++;
}
}
for (int i = 0; i <= n; i++) {
c[i] = d[i] = 0;
}
}
g << result << '\n';
return 0;
}