Cod sursa(job #3333263)

Utilizator abetAlbert Voiculescu abet Data 12 ianuarie 2026 17:30:47
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("strmatch.in");
ofstream out("strmatch.out");
int main() {
    ios::sync_with_stdio(false);
    in.tie(nullptr);

    string A, B;
    in >> A >> B;

    int m = A.size();
    int n = B.size();

    vector<int> pos;
    if (m > n) {
        out << 0 << "\n";
        return 0;
    }

    const uint64_t BASE = 1315423911ULL;

    uint64_t hashA = 0, hashB = 0, power = 1;

    // hash pentru A și prima fereastră din B
    for (int i = 0; i < m; i++) {
        hashA = hashA * BASE + (unsigned char)A[i];
        hashB = hashB * BASE + (unsigned char)B[i];
        if (i < m - 1)
            power *= BASE;
    }

    // sliding window
    for (int i = 0; i <= n - m; i++) {
        if (hashA == hashB) {
            // verificare exactă (siguranță 100%)
            if (B.compare(i, m, A) == 0) {
                if ((int)pos.size() < 1000)
                    pos.push_back(i);
            }
        }

        // mutăm fereastra
        if (i + m < n) {
            hashB -= power * (unsigned char)B[i];
            hashB = hashB * BASE + (unsigned char)B[i + m];
        }
    }

    out << pos.size() << "\n";
    for (int x : pos)
        out << x << " ";
    out << "\n";

    return 0;
}