Cod sursa(job #3195064)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 20 ianuarie 2024 01:21:57
Problema Potrivirea sirurilor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;

ifstream f("strmatch.in");
ofstream g("strmatch.out");

long long p = 1007, mod = 1000000007;
long long hasha, ctr;
long long hashb[2000005];
char a[2000005], b[2000005];
vector < int > rez;
long long pw[2000005];
int n, m;

int main() {
    f >> (a+1) >> (b+1);
    n = strlen(a+1); m = strlen(b+1);
    if (m < n) {
        g << 0;
        return 0;
    }
    pw[1] = 1;
    for (int i = 2; i<=m; i++) {
        pw[i] = (pw[i-1] * p) % mod;
    }
    for (int i=1; i<=n; i++) {
        hasha = (hasha + (a[i]-'A'+1) * pw[i]) % mod;
    }
    for (int i = 1; i<=m; i++) {
        hashb[i] = (hashb[i-1] + (b[i]-'A'+1) * pw[i]) % mod;
    }
    for (int i=1; i+n-1<=m; i++) {
        long long chash = (hashb[i+n-1]+mod-hashb[i-1])%mod;
        if (chash == (hasha*pw[i]) % mod) {
            ctr++;
            if (ctr<1000)
                rez.push_back(i-1);
        }
    }
    g << ctr << '\n';
    for (auto x:rez)
        g << x << " ";
    return 0;
}