Cod sursa(job #1637043)

Utilizator margikiMargeloiu Andrei margiki Data 7 martie 2016 14:33:11
Problema Potrivirea sirurilor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
# include <bits/stdc++.h>
# define NR 2000005
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
int i,j,n,m,la,lb,VV,k,nrsol;
int sol[1005], pi[NR];
char a[NR], b[NR];
int main ()
{
    f.getline (a+1, NR); la=strlen (a+1);
    f.getline (b+1, NR); lb=strlen (b+1);

    for (i=2; i<=la; ++i) {
        while (k>0 && a[k+1]!=a[i])
            k=pi[k];

        if (a[k+1]==a[i]) ++k;
        pi[i]=k;
    }

    k=0;
    for (i=1; i<=lb; ++i) {
        while (k>0 && a[k+1]!=b[i])
            k=pi[k];

        if (a[k+1]==b[i]) ++k;

        if (k==la) {
            ++nrsol;
            if (VV<1000) sol[++VV]=i-la;
            k=pi[k];
        }
    }

    g<<nrsol<<"\n";
    for (i=1; i<=min(VV, 1000); ++i)
        g<<sol[i]<<" ";
    return 0;
}