Cod sursa(job #2385031)

Utilizator alexandruilieAlex Ilie alexandruilie Data 21 martie 2019 15:25:05
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
#include <string.h>
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
char s1[2000005],s2[2000005];
unsigned int hsh1,hsh2,sz1,sz2,sol[2000],x=1,y=1,i,hsh12,hsh22;
long long nr;
int main()
{
    f>>s1>>s2;
    sz1=strlen(s1);
    sz2=strlen(s2);
    for(i=0; i<sz1; i++)
    {
        hsh1=hsh1*33+s1[i];
        hsh2=hsh2*73+s1[i];
    }
    for(i=0; i<sz1; i++)
    {
        hsh12=hsh12*33+s2[i];
        hsh22=hsh22*73+s2[i];
        x=x*33;
        y=y*73;
    }
    if(hsh12==hsh1&&hsh22==hsh2) {nr++;sol[nr]=0;}
    for(i=sz1; i<sz2; i++)
    {
        hsh12=hsh12*33+s2[i]-s2[i-sz1]*x;
        hsh22=hsh22*73+s2[i]-s2[i-sz1]*y;
        if(hsh12==hsh1&&hsh22==hsh2) {nr++;if(nr<=1000)sol[nr]=i-sz1+1;}
    }
    g<<nr<<'\n';
    if(nr>1000) nr=1000;
    for(i=1;i<=nr;i++) g<<sol[i]<<' ';
    return 0;
}