Cod sursa(job #3038861)

Utilizator AleXutzZuDavid Alex Robert AleXutzZu Data 27 martie 2023 21:01:10
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>
#include <vector>

int main() {
    std::ifstream input("strmatch.in");
    std::ofstream output("strmatch.out");

    std::string a, b;
    input >> a >> b;
    std::vector<int> ans;
    auto hasher = std::hash<std::string>();

    size_t a_hash = hasher(a);

    std::string window = b.substr(0, a.length());

    if (hasher(window) == a_hash) ans.push_back(0);

    int len = a.length();

    for (int i = 1; i + len - 1 < b.length(); ++i) {
        window = window.substr(1);
        window += b[len + i - 1];

        if (hasher(window) == a_hash) ans.push_back(i);
    }

    output << ans.size() << '\n';
    for (auto &x: ans) output << x << " ";

    return 0;
}