Cod sursa(job #3202875)

Utilizator indianu_talpa_iuteTisca Catalin indianu_talpa_iute Data 12 februarie 2024 15:49:53
Problema Potrivirea sirurilor Scor 18
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 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] = {0};

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

    int q = 0;
    int n = (int) strlen(a), m = (int) strlen(b);
    for (int i = 1; i < n; i++) {
        while (q && a[i] == a[q])
            q = lps[q + 1];
        if (a[i] == a[q])
            q++;
        lps[i + 1] = q;
    }

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

    fout << matches.size() << '\n';
    for (int i = 0; i < min((int) matches.size(), 1000); i++)
        fout << matches[i] << ' ';
    return 0;
}