Cod sursa(job #1398768)

Utilizator japjappedulapPotra Vlad japjappedulap Data 24 martie 2015 13:15:10
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;

ifstream is ("strmatch.in");
ofstream os ("strmatch.out");

const int Lmax = 2000003;

int N, M, D[Lmax];
char Pattern[Lmax], Text[Lmax];
vector <int> S;

int main()
{
    is >> Pattern;
    is >> Text;
    N = strlen(Pattern);
    M = strlen(Text);
    for (int i = 1, k = 0; i < N; ++i)
    {
        while (k > 0 && Pattern[k] != Pattern[i])
            k = D[k-1];
        if (Pattern[k] == Pattern[i])
            ++k;
        D[i] = k;
    }
    for (int i = 0, k = 0; i < M; ++i)
    {
        while (k > 0 && Pattern[k] != Text[i])
            k = D[k-1];
        if (Pattern[k] == Text[i])
            ++k;
        if (k == N)
            if (S.size() < 1000)
                S.push_back(i-N+1);
    }
    os << S.size() << '\n';
    for (const int& i : S)
        os << i << ' ';




    is.close();
    os.close();
}