Cod sursa(job #1436301)

Utilizator ButnaruButnaru George Butnaru Data 15 mai 2015 18:44:26
Problema Potrivirea sirurilor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <stdio.h>
#include <cstring>
#define p 54
#define mod1 666013
#define mod2 7654321
#define nmax 2000010
using namespace std;
int n,i,j,m,hash1,hash2,sol,curenthash1,curenthash2,soll[nmax],p11,p22;
char s[nmax],ss[nmax];
int main(){
freopen("strmatch.in","r",stdin);
freopen("strmatch.out","w",stdout);
gets(s+1); gets(ss+1);
hash1=0; hash2=0; curenthash1=0; curenthash2=0; p11=p22=1;
n=strlen(s+1); m=strlen(ss+1);
for (i=1;i<=n;i++) {
    hash1=(hash1*p+s[i])%mod1,
    hash2=(hash2*p+s[i])%mod2;
if (i>1) {
    p11=(p11*p)%mod1;
    p22=(p22*p)%mod2;
}
}
if (n>m) { printf("0"); return 0; }
sol=0;
for (i=1;i<=n;i++) curenthash1=(curenthash1*p+ss[i])%mod1,
    curenthash2=(curenthash2*p+ss[i])%mod2;
if (curenthash1==hash1 && curenthash2==hash2) sol=1,soll[1]=0;
for (i=n+1;i<=m;i++){
    curenthash1=((curenthash1-(ss[i-n]*p11)%mod1+mod1)*p+ss[i])%mod1;
    curenthash2=((curenthash2-(ss[i-n]*p22)%mod2+mod2)*p+ss[i])%mod2;
    if (curenthash1==hash1 && curenthash2==hash2) {
        sol++;
        if (sol<=1000) soll[sol]=i-n;
    }
}
printf("%d\n",sol);
if (sol>1000) sol=1000;
for (i=1;i<=sol;i++) printf("%d ",soll[i]);
return 0;
}