Cod sursa(job #3226895)

Utilizator lolismekAlex Jerpelea lolismek Data 23 aprilie 2024 11:26:02
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.05 kb
#include <algorithm>
#include <iostream>
#include <fstream>
#include <climits>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>

#include <iomanip>
#include <cassert>

#include <random>
#include <chrono>

// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

using ull = unsigned long long;
using ll = long long;

//#define int __int128
//#define int ll
#define pii pair <int, int>
#define all(a) (a).begin(), (a).end()
#define fr first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound

#define vt vector
#define FOR(a, b) for(int i = (a); i <= (b); i++)
#define FORr(a, b) for(int i = (a); i >= (b); i--)
#define sz(x) (int)(x).size()

#define YES cout << "YES\n"
#define NO cout << "NO\n"

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int rangerng(int l, int r){
    return uniform_int_distribution<>(l, r)(rng);
}

string filename = "strmatch";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");

////////////////////////////////////////////////////////////////////////////////////

const int NMAX = 4e6;

int n;
string s;

int kmp[NMAX + 1];

void KMP(){
    kmp[1] = 0;
    for(int i = 2; i <= n; i++){
        int l = kmp[i - 1];
        while(l > 0 && s[i] != s[l + 1]){
            l = kmp[l];
        }
        if(s[i] == s[l + 1]){
            kmp[i] = l + 1;
        }
    }
}

void solve(){
    string a, b;
    fin >> a >> b;
    s = "$" + a + "$" + b;

    n = sz(s) - 1;
    KMP();

    int ans = 0;
    vt <int> pos;

    for(int i = sz(a) + 2; i <= n; i++){
        if(kmp[i] == sz(a)){
            ans++;
            if(sz(pos) < 1000){
                pos.pb(i - 2 * sz(a) - 1);
            }
        }
    }

    fout << ans << '\n';
    for(int x : pos){
        fout << x << ' ';
    }
    fout << '\n';
}   


signed main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int T;
    //cin >> T;

    T = 1;

    while(T--){
        solve();
    }

    return 0;
}

/*
*/