Cod sursa(job #627931)

Utilizator andra23Laura Draghici andra23 Data 30 octombrie 2011 23:27:41
Problema Substr Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#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;
}