Cod sursa(job #977983)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 27 iulie 2013 12:57:07
Problema Reguli Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

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

const int N = 2000110;

char P[N], T[N];
int n, m, sol, pi[N];
vector <int> poz;

void Prefix()
{
    int k = 0; pi[1] = 0;
    for(int q=2; q<=m; q++)
    {
        while(k && P[k+1] != P[q])
            k = pi[k];
        if(P[k+1] == P[q]) k++;
        pi[q] = k;
    }
}

void Compute()
{
    int q = 0;
    for(int i=1; i<=n; i++)
    {
        cout<<q<<endl;
        while(q && P[q+1] != T[i])
            q = pi[q];
        if(P[q+1] == T[i]) q++;
        if(q == m)
        {
            sol++;
            q = pi[q];
            if(sol <= 1000) poz.push_back(i-m);
        }
    }
}

int main()
{
    fin.getline(P, N); fin.getline(T, N);
    m = strlen(P), n = strlen(T);
    for(int i=n; i>0; i--) T[i] = T[i-1]; T[0] =' ';
    for(int i=m; i>0; i--) P[i] = P[i-1]; P[0] =' ';

    Prefix(); Compute();

    fout<<sol<<'\n';
    sol = min(sol, 1000);
    for(unsigned i=0; i<sol; i++)
        fout<<poz[i]<<' ';
    return 0;
}