Cod sursa(job #2963527)

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

using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
string a,b;
int z[4000005];
void z_function()
{
    int n=b.size();
    z[0]=0;
    int l=0;
    int r=0;
    for(int i=1;i<n;i++)
    {
        if(r>=i)
            z[i]=min(z[i-l],r-i+1);
        while(i+z[i]<n&&b[z[i]]==b[i+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;
    b=a+"#"+b;
    z_function();
    int ans=0;
    for(int i=a.size();i<b.size();i++)
        if(z[i]==a.size())
            ans++;
    fout<<ans<<'\n';
    int cnt=0;
    for(int i=a.size();i<b.size()&&cnt<1000;i++)
        if(z[i]==a.size())
        {
            cnt++;
            fout<<i-1-(int)a.size()<<' ';
        }
    return 0;
}