Cod sursa(job #2398562)

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

const int P=73;
string a,b;
int k;
vector<int>V;
int ha,hb,db=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) {
        ha=(ha*P+a[i])%M;
        hb=(hb*P+b[i])%M;
        if (i) db=(db*P)%M;
    }
    if (ha==hb) {
        ++k; V.push_back(0);
    }
    for (int i=a.size(); i<b.size(); ++i) {
        hb=(hb-(db*b[i-a.size()])%M+M)%M;
        hb=(hb*P+b[i])%M;
        if (ha==hb) {
            ++k;
            if (k<=1000) V.push_back(i);
        }
    }

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


    return 0;
}