Cod sursa(job #1378354)

Utilizator andreismara97Smarandoiu Andrei andreismara97 Data 6 martie 2015 11:46:22
Problema Potrivirea sirurilor Scor 36
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include<fstream>
#include<cstring>
using namespace std;
ifstream in("strmatch.in");
ofstream out("strmatch.out");

#define N 2000003

char x[N], y[N];
int Pi[N], d[N];
int k, n, m;

void constructie_pi()
{
    Pi[1] = 0;
    k = 0;
    for(int i=2; i<=n; i++)
    {
        while( k>0 && x[i] != x[k+1])
            k=Pi[k];

        if(x[i] == x[k+1])
            k++;

        Pi[i] = k;
    }
}

void citire()
{
    in.getline(x, N);
    in.getline(y, N);

    n=strlen(x)-1;
    m=strlen(y)-1;
}

int main ()
{
    citire();

    constructie_pi();   //construim Pi-ul

    k=0;
    for(int i=1; i<=m; i++)
    {
        while( k>0 && y[i] != x[k+1])
            k=Pi[k];

        if( y[i] == x[k+1])
            k++;

        d[i] = k;
    }

    int nr = 0;
     for(int i=1; i<=m; i++)
        if( d[i] == n )
            nr++;

    out<<nr<<'\n';
    nr=1;
    for(int i=1; i<=m; i++)
    {
        if( d[i] == n && n<=1000)
        {
            out<<i-n<<' ';
            nr++;
        }
        else
            if(n>1000)
                return 0;
    }
    return 0;
}