Cod sursa(job #2398563)

Utilizator greelioGreenio Greely greelio Data 5 aprilie 2019 18:47:38
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include<bits/stdc++.h>
#define M1 1000003
#define M2 1000033
#define int long long
using namespace std;

const int P=73;
string a,b;
int k;
vector<int>V;
int ha1,ha2,hb1,hb2,db1=1,db2=1;


int32_t main() {
    ifstream cin("strmatch.in");
    ofstream cout("strmatch.out");
    cin>>a>>b;

    if (a.size()>b.size()) return cout<<0,0;

    for (int i=0; i<a.size(); ++i) {
        ha1=(ha1*P+a[i])%M1;
        hb1=(hb1*P+b[i])%M1;
        if (i) db1=(db1*P)%M1;

        ha2=(ha2*P+a[i])%M2;
        hb2=(hb2*P+b[i])%M2;
        if (i) db2=(db2*P)%M2;
    }
    if (ha1==hb1 && ha2==hb2) {
        ++k; V.push_back(0);
    }
    for (int i=a.size(); i<b.size(); ++i) {
        hb1=(hb1-(db1*b[i-a.size()])%M1+M1)%M1;
        hb1=(hb1*P+b[i])%M1;

        hb2=(hb2-(db2*b[i-a.size()])%M2+M2)%M2;
        hb2=(hb2*P+b[i])%M2;
        if (ha1==hb1 && ha2==hb2) {
            ++k;
            if (k<=1000) V.push_back(i-a.size()+1);
        }
    }

    cout<<k<<'\n';
    for (auto it:V) cout<<it<<" ";


    return 0;
}