Cod sursa(job #1374508)

Utilizator serban_ioan97Ciofu Serban serban_ioan97 Data 5 martie 2015 09:41:20
Problema Potrivirea sirurilor Scor 38
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <cstring>
#define nmax 2000020
using namespace std;

ifstream f("strmatch.in");
ofstream g("strmatch.out");

char a[nmax], b[nmax];
int i, n, m, j, q, k, nxt[nmax], position[nmax];

int main()
{
    f>>(a+1);
    f>>(b+1);

    n=strlen(a+1);
    m=strlen(b+1);

    nxt[1]=0; q=0;

    for(i=2; i<=n; ++i)
    {
        while(a[i]!=a[q+1] && q!=0)
            q=nxt[q];

        if(a[q+1]==a[i]) ++q;

        nxt[i]=q;
    }

    for(i=2, q=0; i<=m; ++i)
    {
        while(b[i]!=a[q+1] && q)
            q=nxt[q];

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

        if(q==n)
        {
            q=nxt[q];
            ++k;
            if(k<1000) position[k]=i-n;
        }
    }

    g<<k<<"\n";

    for(i=1; i<=min(k, 1000); ++i)
        g<<position[i]<<" ";

    return 0;
}