Cod sursa(job #2229205)

Utilizator Rodik_RodyRodica Vasilescu Rodik_Rody Data 6 august 2018 11:52:30
Problema Potrivirea sirurilor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>
#define minim(a, b) ((a < b) ? a : b)
#define NMax 2000005
using namespace std;
ifstream in("strmatch.in");
ofstream out("strmatch.out");
int M, N, q ,n;
string substring, str, prefix, pos;

void make_prefix(void)
{

    for (int i = 2, q = 0; i <= M; ++i){
        while (q && substring[q+1] != substring[i])
            q = prefix[q];
        if (substring[q+1] == substring[i])
            ++q;
        prefix[i] = q;
    }
}

int main()
{
    in >> substring >> str;

    M = substring.length();
    N = str.length();
    str.insert(0," ");
    substring.insert(0," ");

    make_prefix();

    for (int i = 1; i <= N; ++i)
    {
        while (q && substring[q+1] != str[i])
            q = prefix[q];
        if (substring[q+1] == str[i])
            ++q;
        if (q == M)
        {
            q = prefix[M];
            ++n;
            pos[n] = i-M;
        }
    }

    out<< n <<endl;;
    for (int i = 1; i <= minim(n, 1000); ++i)
        cout<< pos[i] <<" ";
    cout<<endl;

    return 0;
}