Cod sursa(job #222381)

Utilizator free2infiltrateNezbeda Harald free2infiltrate Data 22 noiembrie 2008 09:50:31
Problema Potrivirea sirurilor Scor 78
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <stdio.h>
#include <string.h>

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

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


int main()
{

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

scanf("%s %s",A,B);
NA = strlen(A);
NB = strlen(B);

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

for (int i=0;i<NA;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 (NA>NB)
{
printf("0\n");
return 0;
}

int hash1=0,hash2=0;

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


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


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

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

Nr=0;
for (int i = 0;i<NB && Nr<1000;i++)
if (match[i]) Nr++,printf("%d ",i);



return 0;
}