Cod sursa(job #3214851)

Utilizator not_anduAndu Scheusan not_andu Data 14 martie 2024 15:07:50
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#pragma GCC optimize ("03", "Ofast", "unroll-loops")
#include <bits/stdc++.h>

using namespace std;

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

typedef long long ll;

vector<int> zFunction(string s) {

    int n = s.length();
    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, aux; cin >> pattern >> aux;
    string s;
    s += pattern;
    s += '#';
    s += aux;

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

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

    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;
}