Cod sursa(job #3038867)

Utilizator AleXutzZuDavid Alex Robert AleXutzZu Data 27 martie 2023 21:08:03
Problema Potrivirea sirurilor Scor 16
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 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;

    int sum_check = 0;
    int window = 0;
    int a_len = a.length();
    int b_len = b.length();

    for (int i = 0; i < a_len; ++i) {
        sum_check += a[i];
        window += b[i];
    }

    if (window == sum_check) ans.push_back(0);

    for (int i = 1; i + a_len - 1 < b_len; ++i) {
        window -= b[i - 1];
        window += b[i + a_len - 1];

        if (window == sum_check) ans.push_back(i);
    }

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

    return 0;
}