Cod sursa(job #1813030)

Utilizator rangalIstrate Sebastian rangal Data 22 noiembrie 2016 17:34:47
Problema Potrivirea sirurilor Scor 78
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <cstdio>
#include <iostream>
#include <cstring>
#define lgmax 2000003
#define in "strmatch.in"
#define out "strmatch.out"
#define base 73
#define mod 100007
#define mod2 100021

typedef unsigned long long ull;

using namespace std;

ull Mx,Mx2;
ull ha,ha2,hb,hb2;
char A[lgmax],B[lgmax];
int ct;
bool match[lgmax];

int m,n;

int main()
{
    freopen(in,"r",stdin);
    freopen(out,"w",stdout);

    scanf("%s %s",A,B);

    m=strlen(A);
    n=strlen(B);

    if(m>n)
    {
        printf("0\n");
        return 0;
    }

    Mx=Mx2=1;

    for(int i=0; i<m; ++i)
    {
        ha=(ha*base +A[i])%mod;
        ha2=(ha2*base +A[i])%mod2;
        hb=(hb*base +B[i])%mod;
        hb2=(hb2*base +B[i])%mod2;

        if(i!=0) //Mx = base^(m-1) %mod , Mx2 =... %mod2
        {
            Mx=(Mx*base)%mod;
            Mx2=(Mx2*base)%mod2;
        }
    }

    for(int i=0; i<=n-m; ++i)
    {
        if(ha==hb && ha2==hb2)
        {
            ++ct;
            match[i]=1;
        }
        hb=( (hb - (Mx*B[i])%mod +mod) * base +B[i+m] ) %mod;
        hb2=( (hb2 - (Mx2*B[i])%mod2 +mod2) * base +B[i+m] ) %mod2;
    }

    printf("%d\n",ct);
    ct=0;
    for(int i=1; ct<1000 && i<=n; ++i)
        if(match[i]==1)
           printf("%d ",i) ,++ct;

    fclose(stdin); fclose(stdout);
    return 0;
}