Cod sursa(job #3176904)

Utilizator paull122Paul Ion paull122 Data 28 noiembrie 2023 00:18:32
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define MOD 666013

#define P 73
#define HSIZE1 100021
#define HSIZE2 100007

ifstream fin("strmatch.in");
ofstream fout("strmatch.out");


char a[2000001];
char b[2000001];
int main()
{
    fin >> a >> b;
    if(strlen(a) > strlen(b))
    {
        fout << 0;
        return 0;
    }


    int put1=1,put2=1;
    int strhash1=0,strhash2=0;
    int pathash1=0,pathash2=0;
    int len = strlen(a);

    for(int i=0;i<len;i++)
    {
        pathash1 = (pathash1 * P + a[i])%HSIZE1;
        pathash2 = (pathash2 * P + a[i])%HSIZE2;

        if(i)
        {
            put1 = put1 * P % HSIZE1;
            put2 = put2 * P % HSIZE2;
        }

    }
    for(int i=0;i<len;i++)
    {
        strhash1= (strhash1 * P + b[i])%HSIZE1;
        strhash2 = (strhash2 * P + b[i])%HSIZE2;
    }
    vector<int> r;
    if(strhash1 == pathash1 && strhash2 == pathash2)
    {
        r.push_back(0);
    }
    for(int i=len;i<strlen(b);i++)
    {
        strhash1 = ((strhash1 - (b[i-len]*put1) % HSIZE1 + HSIZE1)*P + b[i])%HSIZE1;
        strhash2 = ((strhash2 - (b[i-len]*put2) % HSIZE2 + HSIZE2)*P + b[i])%HSIZE2;
        if(strhash1 == pathash1 && strhash2 == pathash2 && r.size() < 1000)
        {
            r.push_back(i-len+1);
        }
    }
    int n=r.size();
    fout << n << "\n";
    for(int i : r)
    {
        fout << i << " ";
    }
}