Cod sursa(job #222528)

Utilizator free2infiltrateNezbeda Harald free2infiltrate Data 23 noiembrie 2008 10:35:11
Problema Potrivirea sirurilor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <stdio.h>
#include <string.h>

#define P 73
#define maxn 2000001
#define mod1 100007
#define mod2 100021

char A[maxn],B[maxn],match[maxn];
int P1,P2,hashA1,hashA2;


int main()
{
FILE *in = fopen("strmatch.in","r");
FILE *out = fopen("strmatch.out","w");

P1=P2=1;
hashA1=hashA2=0;

fscanf(in,"%s %s",A,B);

int i,la,lb;
la = strlen(A);
lb = strlen(B);

for (i=0;i<la;i++)
{
hashA1 = (hashA1*P+A[i])%mod1;
hashA2 = (hashA2*P+A[i])%mod2;

if (i!=0)
P1 = (P1*P)%mod1,
P2 = (P2*P)%mod2;
}

if (la>lb)
{
fprintf(out,"0");
return 0;
}

int hash1=0,hash2=0;

for (i=0;i<la;i++)
hash1 = (hash1*P+B[i])%mod1,
hash2 = (hash2*P+B[i])%mod2;

int Nr=0;

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


  
for (i=la;i<lb;i++)
{
hash1 = ((hash1-(B[i-la]*P1)%mod1+mod1)*P+B[i])%mod1;
hash2 = ((hash2-(B[i-la]*P2)%mod2+mod2)*P+B[i])%mod2;


if (hash1==hashA1 && hash2==hashA2)
Nr++,match[i-la+1]=1;
}

fprintf(out,"%d\n",Nr);
Nr=0;
for (i=0;i<lb && Nr<1000;i++)
if (match[i]) Nr++,fprintf(out,"%d ",i);
return 0;
}