Cod sursa(job #3149321)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 7 septembrie 2023 12:07:55
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
string a,b;
int n,m,z[4000005];
void zfunc()
{
    z[0]=0;
    int l=0;
    int r=0;
    for(int i=1;i<b.size();i++)
    {
        if(i<=r)
            z[i]=min(z[i-l],r-i);
        while(i+z[i]<b.size()&&b[i+z[i]]==b[z[i]])
            z[i]++;
        if(i+z[i]-1>r)
        {
            l=i;
            r=i+z[i]-1;
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);
    fin>>a>>b;
    n=a.size();
    m=b.size();
    b=a+"#"+b;
    zfunc();
    int ans=0;
    vector<int> sol;
    for(int i=a.size();i<b.size();i++)
        if(z[i]==a.size())
        {
            ans++;
            int x=i-a.size()-1;
            if(sol.size()<1000)
                sol.push_back(x);
        }
    fout<<ans<<'\n';
    for(auto i:sol)
        fout<<i<<' ';
    return 0;
}