Cod sursa(job #3333266)

Utilizator abetAlbert Voiculescu abet Data 12 ianuarie 2026 17:33:39
Problema Potrivirea sirurilor Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 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();

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

    const uint64_t BASE = 1315423911ULL;

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


    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;
    }

    vector<int> pos;
    long long totalMatches = 0;

    for (int i = 0; i <= n - m; i++) {
        if (hashA == hashB) {

            if (B.compare(i, m, A) == 0) {
                totalMatches++;
                if ((int)pos.size() < 1000)
                    pos.push_back(i);
            }
        }


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

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

    return 0;
}