Cod sursa(job #3225418)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 17 aprilie 2024 16:10:45
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
string a,b,s;
vector<int> sol;
int ans=0;
int pi[4000005];
void prefix()
{
    pi[0]=0;
    for(int i=1;i<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;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);
    fin>>a>>b;
    s=a+"#"+b;
    prefix();
    for(int i=a.size();i<s.size();i++)
        if(pi[i]==a.size())
        {
            ans++;
            if(sol.size()<1000)
                sol.push_back(i-2*(int)a.size());
        }
    fout<<ans<<'\n';
    for(int i:sol)
        fout<<i<<' ';
    return 0;
}