Cod sursa(job #2089100)

Utilizator MarianConstantinMarian Constantin MarianConstantin Data 16 decembrie 2017 10:54:43
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>
#include <cstring>
#define np1 2000003
#define np2 2000029

using namespace std;

char p[2000020], s[2000020];

int main()
{
    ifstream fin("strmatch.in");
    ofstream fout("strmatch.out");
    int v[1010], i, h1s=0, h2s=0, h1p=0, h2p=0, pn1=1, pn2=1, nr=0, n, m;
    fin.getline(p, 2000020);
    fin.getline(s, 2000020);
    n=strlen(s);
    m=strlen(p);
    for (i=0; i<m; i++)
    {
        pn1=(pn1*256)%np1;
        pn2=(pn2*256)%np2;
        h1s=((h1s*256)%np1+s[i]%np1)%np1;
        h2s=((h2s*256)%np2+s[i]%np2)%np2;
        h1p=((h1p*256)%np1+p[i]%np1)%np1;
        h2p=((h2p*256)%np2+p[i]%np2)%np2;
    }
    for (i=m-1; i<n; i++)
    {
        if (h1s==h1p && h2s==h2p)
        {
            if (nr<1000)
                v[nr++]=i-m+1;
        }
        h1s=((h1s*256)%np1-(s[i+1-m]*pn1)%np1+s[i+1])%np1;
        h1s=(np1+h1s)%np1;
        h2s=((h2s*256)%np2-(s[i+1-m]*pn2)%np2+s[i+1])%np1;
        h2s=(np2+h2s)%np2;
    }
    fout << nr << "\n";
    for (i=0; i<nr; i++)
        fout << v[i] << " ";
    return 0;
}