Cod sursa(job #3363914)

Utilizator adimiclaus15Miclaus Adrian Stefan adimiclaus15 Data 25 august 2026 10:55:30
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int MOD = 1e9 + 7;
const int BASE = 67;
const int NMAX = 2e6;

//int B[NMAX + 1]; //B[i] = BASE^i
int H[NMAX + 1];

int expo(int a, int n) {
    if(n == 0) {
        return 1;
    } else {
        if(n % 2) {
            return (a * expo(a, n - 1)) % MOD;
        } else {
            int t = expo(a, n / 2);
            return (t * t) % MOD;
        }
    }
}

int inv(int a) {
    return expo(a, MOD - 2);
}

int cod(char c) {
    if(c >= 'A' && c <= 'Z') {
        return c - 'A' + 1;
    }
    if(c >= 'a' && c <= 'z') {
        return c - 'a' + 27;
    }
    return c - '0' + 53;
}

int get_hash(int st, int dr) {
    int h = (H[dr] - H[st - 1] + MOD) % MOD;
    h = (h * inv(expo(BASE, st - 1))) % MOD;
    return h;
}

signed main() {
    ifstream cin("strmatch.in");
    ofstream cout("strmatch.out");
	string p, t;
    cin >> p;
    cin >> t;
    int n = t.size();
    int m = p.size();
    p = '#' + p;
    t = '$' + t;
    int hp = 0;
    for(int i = 1; i <= m; i++) {
        hp = (hp + cod(p[i]) * expo(BASE, i)) % MOD;
    }
    for(int i = 1; i <= n; i++) {
        H[i] = (H[i - 1] + cod(t[i]) * expo(BASE, i)) % MOD;
    }
    vector<int> v;
    for(int i = 1; i <= n - m + 1; i++) {
        if(hp == get_hash(i, i + m - 1)) {
            v.push_back(i - 1);
        }
    }
    cout << v.size() << '\n';
    int sz = v.size();
    if(sz > 1000) {
        sz = 1000;
    }
    for(int i = 0; i < sz; i++) {
        cout << v[i] << ' ';
    }
}