Cod sursa(job #1472924)

Utilizator theprdvtheprdv theprdv Data 18 august 2015 03:56:02
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <string.h>
#include <string>

#define MAXN 2000001
#define P 73
#define MOD 100007
#define min(x, y) (((x) < (y)) ? (x) : (y))

char A[MAXN], B[MAXN];
int NA, NB, ans;

int hashA1, hashA2, P1 = 1;
bool match[MAXN];

int main()
{
	freopen("strmatch.in", "rt", stdin);
	freopen("strmatch.out", "wt", stdout);

	gets(A), gets(B);
	NA = strlen(A), NB = strlen(B);

	if (NA > NB) { printf("0"); return 0; }

	for (int i = 0; i < NA; ++i){
		if (i == NA - 1) hashA2 = hashA1;
		hashA1 = (hashA1 * P + A[i]) % MOD;
		if (i) P1 = (P1 * P) % MOD;
	}

	int hash1 = 0, hash2 = 0;
	for (int i = 0; i < NA; ++i){
		if (i == NA - 1) hash2 = hash1;
		hash1 = (hash1 * P + B[i]) % MOD;
	}

	if (hashA1 == hash1 && hashA2 == hash2) ++ans, match[0] = 1;

	for (int i = NA; i < NB; ++i){
		hash1 = (hash1 - (B[i - NA] * P1) % MOD + MOD) % MOD;
		hash2 = hash1;
		hash1 = (hash1 * P + B[i]) % MOD;

		if (hash1 == hashA1 && hash2 == hashA2) match[i - NA + 1] = 1, ++ans;
	}
	printf("%d\n", ans);

	for (int i = 0, j = 1; j <= min(ans, 1000); ++i)
		if (match[i]) printf("%d ", i), ++j;

	return 0;
}