Cod sursa(job #2658848)

Utilizator Gheorghita_VladGheorghita Vlad Gheorghita_Vlad Data 15 octombrie 2020 12:00:25
Problema Potrivirea sirurilor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
int lps[2000010],v[2000010],n,m;
char A[2000010],B[2000010];
void BuildLPS()
{
    int i,k=0;
    lps[1]=0;
    for (i=2;i<=n;i++)
    {
        while (k>0 && B[i-1]!=B[k]) k=lps[k];
        if (B[i-1]==B[k]) k++;
        lps[i]=k;
    }
}
void KMP()
{
    int k=0,q=0,i;
    for (i=0;i<m;i++)
    {
        while (q>0 && A[i]!=B[q]) q=lps[q];
        if (B[q]==A[i]) q++;
        if (q==n && k<1000) v[++k]=i-n+1;
        else if (k>=1000) k++;
    }
        g<<k<<'\n';
    for (i=1;i<=k && i<=1000;i++) g<<v[i]<<" ";
}
int main()
{
    f.getline(B);
    f.getline(A);
    n=strlen(B);
    m=strlen(A);
    BuildLPS();
    KMP();
    return 0;
}