Cod sursa(job #2307672)

Utilizator filicriFilip Crisan filicri Data 25 decembrie 2018 13:26:33
Problema Potrivirea sirurilor Scor 32
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>
#include <string>
#define d 256
#define q 100007
#define q1 100021
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
string a,b;
int n,m,ha,hb,h,j,sol[2000004],ha1,hb1,h1;
int main()
{
    f>>a>>b;
    m=a.length();
    n=b.length();
    h=h1=1;
    for (int i=0;i<m;i++)
    {
        ha=(d*ha+a[i])%q;
        hb=(d*hb+b[i])%q;
        ha1=(d*ha1+a[i])%q1;
        hb1=(d*hb1+b[i])%q1;
        if (i<m-1)
        {
            h=(h*d)%q;
            h1=(h1*d)%q1;
        }
    }
    for (int i=0;i<=n-m;i++)
    {
        if (ha==hb && ha1==hb1)
        {
            sol[0]++;
            sol[sol[0]]=i;
        }
        if (i<n-m)
        {
            hb=(d*(hb-b[i]*h)+b[i+m])%q;
            hb1=(d*(hb1-b[i]*h1)+b[i+m])%q1;
            if (hb<0) hb+=q;
            if (hb1<0) hb1+=q1;
        }
    }
    g<<sol[0]<<'\n';
    sol[0]=min(sol[0],1000);
    for (int i=1;i<=sol[0];i++) g<<sol[i]<<' ';
    f.close();
    g.close();
    return 0;
}