Cod sursa(job #2866601)

Utilizator cdenisCovei Denis cdenis Data 9 martie 2022 20:29:00
Problema Potrivirea sirurilor Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

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

const int MAX=2e6+5;
int pf[MAX],poz[MAX],i,j,cnt,n;
char a[MAX],b[MAX];

int main()
{
    fin >> a >> b;
    n=strlen(a);
    for(int i=0;a[i];i++)
    j=0; i=1;
    while(a[i])
    {
        while(a[i]!=a[j] && j)
            j=pf[j-1];
        if(a[i]==a[j])
            pf[i++]=(j++)+1;
        else
            i++;
    }
    i=j=0;
    while(b[i])
    {
        while(a[j]!=b[i])
            i++;
        while(a[j]==b[i] && a[j] && b[i])
            i++, j++;
        if(!a[j])
        {
            poz[++cnt]=i-n;
            j=pf[j-1];
        }
        else
            while(b[i]!=a[j] && j)
                j=pf[j-1];
    }
    fout << cnt << '\n';
    for(i=1;i<=cnt;i++)
        fout << poz[i] << " ";
    return 0;
}