Cod sursa(job #3292933)

Utilizator matei__bBenchea Matei matei__b Data 9 aprilie 2025 19:43:09
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#define ll long long
#define ull unsigned long long
#define ld long double
#define chad char
#define mod 1'000'000'007
//const int mod=1'000'000'007;
#define dim 100005
#define lim 1000000
#define BASE 31
#define NMAX 21'005
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define nr_biti __builtin_popcount
using namespace __gnu_pbds; 
using namespace std;

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

int t=1;
string a,b;
int pi[4*lim+5];

void get_pi(const string &s,int pi[])
{
    for(int i=1; i<(int)s.size(); i++)
    {
        int j=pi[i-1];
        while(j>0 && s[i]!=s[j])
            j=pi[j-1];

        if(s[i]==s[j])
            j++;
        pi[i]=j;
    }
}

void kmp(const string &a,const string &b)
{
    vector<int> occ;
    string s=a+"&"+b;
    get_pi(s,pi);

    for(int i=(int)a.size()+1; i<(int)s.size(); i++)
    {
        if(pi[i]==(int)a.size())
            occ.pb(i-2*((int)a.size()));
    }

    fout << (int)occ.size() << "\n";
    for(auto it:occ)
        fout << it << " ";
}

void solve()
{
    fin >> a >> b;    
    kmp(a,b);
}   

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);

    //fin >> t;
    while(t--)
        solve();

    return 0;
}