Cod sursa(job #2385008)

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

    for(i=1;i<=min(nr,1000);i++) g<<sol[i]<<' ';
    return 0;
}