Cod sursa(job #3176903)

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

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

#define HBASE1 73
#define HBASE2 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))
    {
        cout << 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++)
    {
        if(i)
        {
            put1 = put1 * HBASE1 % HSIZE1;
            put2 = put2 * HBASE2 % HSIZE2;
        }
        pathash1 = (pathash1 * HBASE1 + a[i])%HSIZE1;
        pathash2 = (pathash2 * HBASE2 + a[i])%HSIZE2;
        strhash1 = (strhash1 * HBASE1 + b[i])%HSIZE1;
        strhash2 = (strhash2 * HBASE2 + 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)*HBASE1 + b[i])%HSIZE1;
        strhash2 = ((strhash2 - b[i-len]*put2 % HSIZE2 + HSIZE2)*HBASE2 + b[i])%HSIZE2;
        if(strhash1 == pathash1 && strhash2 == pathash2)
        {
            r.push_back(i-len+1);
        }
    }
    int n=r.size();
    fout << n << "\n";
    for(int i=0;i<min(n,1000);i++)
    {
        fout << r[i] << " ";
    }
}