Cod sursa(job #3204272)

Utilizator not_anduAndu Scheusan not_andu Data 16 februarie 2024 09:34:24
Problema Potrivirea sirurilor Scor 28
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>

using namespace std;

#define INFILE "strmatch.in"
#define OUTFILE "strmatch.out"

vector<int> zFunction(string s){
    int n = s.size();
    vector<int> z(n);
    int left = 0, right = 0;
    for(int i = 1; i < n; ++i){
        if(i < right) z[i] = min(right - i, z[i - left]);
        while(i + z[i] < n && s[z[i]] == s[i + z[i]])
            ++z[i];
        if(i + z[i] > right){
            left = i;
            right = i + z[i];
        }
    }
    return z;
}

void solve(){

    string pattern, s;
    cin >> pattern >> s;

    string aux = pattern + s;

    vector<int> ans;
    vector<int> z = zFunction(aux);

    for(int i = 0; i < z.size(); ++i){
        if(z[i] == pattern.size()) ans.push_back(i - pattern.size());
    }

    cout << ans.size() << '\n';
    for(auto it : ans) cout << it << " ";

}

int main(){
    ios_base::sync_with_stdio(false);
    freopen(INFILE, "r", stdin);
    freopen(OUTFILE, "w", stdout);
    cin.tie(0), cout.tie(0);
    solve();
    return 0;
}