Cod sursa(job #2983882)

Utilizator indianu_talpa_iuteTisca Catalin indianu_talpa_iute Data 23 februarie 2023 11:32:49
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>
#define MAXSZ 2000005

using namespace std;

ifstream fin("strmatch.in");
ofstream fout("strmatch.out");

char a[MAXSZ], b[MAXSZ];
int lps[MAXSZ];

int main() {
    fin.getline(a + 1, MAXSZ);
    fin.getline(b + 1, MAXSZ);

    int aSz = (int) strlen(a + 1), bSz = (int) strlen(b + 1);
    int length = 0;
    for (int i = 2; i <= aSz; i++) {
        while (length > 0 && a[length + 1] != a[i])
            length = lps[length];
        if (a[length + 1] == a[i])
            length++;
        lps[i] = length;
    }

    vector<int> matches;
    length = 0;
    for (int i = 1; i <= bSz; i++) {
        while (length > 0 && a[length + 1] != b[i])
            length = lps[length];
        if (a[length + 1] == b[i])
            length++;
        if (length == aSz) {
            matches.push_back(i - aSz);
            length = lps[length];
        }
    }

    fout << matches.size() << '\n';
    for (auto& match: matches)
        fout << match << ' ';
    return 0;
}