Cod sursa(job #1067064)

Utilizator mikeshadowIon Complot mikeshadow Data 26 decembrie 2013 11:23:19
Problema Potrivirea sirurilor Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <string.h>
#include <queue>
#include <math.h>
#include <set>
 
#define min(a,b) ((a<b)?a:b)
#define max(a,b) ((a<b)?b:a)
#define abs(a) ((a<0)?-a:a)
#define REP(i,a,b) \
	for (int i=a; i<=b; i++)
 
#define INF 10000000000001
 
using namespace std;
 

#ifndef TEST
ifstream fin ("strmatch.in");
ofstream fout ("strmatch.out");
#else
ifstream fin ("input.txt");
ofstream fout ("output.txt");
#endif
 
#define MAXN 100000

typedef long long ll;

string data;
string key;
int dl,kl;

const int MOD = 100007;
const int P = 73;
int hashk=0,hashd=0;
int c=1;

bool check (int x)
{
	for (int i=0; i<kl; i++) 
		if (key[i]!=data[x-kl+1+i]) return false;
	return true;
}

vector<int> sol;

int main()
{
	fin>>key;
	fin>>data;
	dl = data.length();
	kl = key.length();

	for (int i=0; i<kl; i++)
		c=(c*P)%MOD;

	if (kl>dl) 
	{
		fout<<sol.size();
		return 0;
	}

	for (int i=0; i<kl; i++)
		hashk = (hashk*P+key[i])%MOD;

	for (int i=0; i<kl; i++)
		hashd = (hashd*P+data[i])%MOD;

	if ((hashd == hashk)  && check(kl-1)) sol.push_back(0);

	for (int i=kl; i<dl; i++)
	{
		hashd = ((hashd*P - (data[i - kl] * c) % MOD + MOD) + data[i]) % MOD;
		if ((hashd == hashk)  && check(i)) sol.push_back(i-kl+1);
	}

	fout<<sol.size();
	fout<<'\n';

	REP(i,0,min(sol.size()-1,999))
		fout<<sol[i]<<' ';


    return 0;
}