Cod sursa(job #786923)

Utilizator SchumiDumitru Andrei Georgian Schumi Data 12 septembrie 2012 13:02:47
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <cassert>
#include <cstdio>
#include <cstring>

#include <algorithm>
using namespace std;

typedef long long int64;

const int N = 2000005;
const int MOD = 999983;
//const int MOD2 = 31309;
const int BASE = 123;

char text[N], pattern[N];
int textHash, patternHash;
int putere[N];
int sol[N];

void read() {
	assert(freopen("strmatch.in", "r", stdin) != NULL);
	assert(freopen("strmatch.out", "w", stdout) != NULL);
	
	assert(scanf("%s %s", pattern + 1, text + 1) == 2);
}

void solve() {
	int n = strlen(pattern + 1);
	int m = strlen(text + 1);
	
	putere[0] = 1;
	for (int i = 1; i < n; ++i)
		putere[i] = (putere[i - 1] * BASE) % MOD;
	
	for (int i = 1; i <= n; ++i)
		patternHash = (patternHash + (pattern[i] * putere[n - i])) % MOD;
	
	for (int i = 1; i <= n; ++i)
		textHash = (textHash + (text[i] * putere[n - i])) % MOD;
		
		
	
	int i = n;
	while (i <= m) {
		if (textHash == patternHash)
			sol[++sol[0]] = i - n;
		textHash = textHash - text[i - n + 1] * putere[n - 1];
		textHash = (textHash % MOD + MOD) % MOD;
		++i;
		textHash = (textHash * BASE + text[i]) % MOD;
	}
}

void write() {	
	printf("%d\n", sol[0]);
	for (int i = 1; i <= min(sol[0], 1000); ++i)
		printf("%d ", sol[i]);
}

int main() {
	read();
	solve();
	write();
}