Cod sursa(job #2865130)

Utilizator CristianPavelCristian Pavel CristianPavel Data 8 martie 2022 15:31:42
Problema Potrivirea sirurilor Scor 14
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream cin("strmatch.in");
ofstream cout("strmatch.out");
int v[2000001];
char s1[2000001];
char s2[2000001];
int nr=0;
void rabin_karp(char s1[], char s2[])
{
    int l1=strlen(s1), l2=strlen(s2);
    int mod1=100007, mod2=100021;
    int h1=0,h2=0,h3=0,h4=0;
    int putere1=1, putere2=1;
    int baza=73;
    for(int i=0;i<l1;i++)
    {
        if(i!=0) putere1=(putere1*baza)%mod1;
        if(i!=0) putere2=(putere2*baza)%mod2;
        h1=(h1*baza+s1[i])%mod1;
        h2=(h2*baza+s1[i])%mod2;
        h3=(h3*baza+s2[i])%mod1;
        h4=(h4*baza+s2[i])%mod2;
    }
    for(int i=0;i<=l2-l1;i++)
    {
        cout<<h1<<" "<<h2<<" "<<h3<<" "<<h4<<endl;
        if(h1==h3 && h2==h4){
            nr++;
            v[i]=1;
        }
            h3 = ((h3 - (s2[i] * putere1) % mod1 + mod1) * baza + s2[i+l1]) % mod1;
		    h4 = ((h4 - (s2[i] * putere2) % mod2 + mod2) * baza + s2[i+l1]) % mod2;
    }
}
int main()
{
    cin.get(s1,2000001);
    cin.get();
    cin.get(s2,2000001);
    if(strlen(s1)>strlen(s2)){
        cout<<"0";
        return 0;
    }
    rabin_karp(s1,s2);
    cout<<nr<<" "<<endl;
    nr=0;
    for(int i=0;i<=strlen(s2) && nr<1000;i++)
    {
        if(v[i]) cout<<i<<" ";
        nr++;
    }
    cin.close();
    cout.close();
    return 0;

}