Cod sursa(job #1963038)

Utilizator andrei232000Andrei Maria andrei232000 Data 12 aprilie 2017 11:30:51
Problema Potrivirea sirurilor Scor 12
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
char sir[100003];
int L, l, k, j, contor;
short p[1003], ap[1003];
char c[1003];
int main()
{

    fin>>c;
    fin>>sir;
    l = strlen(c);
    L = strlen(sir);
        k = 0;
        for(j = 1; j < l; ++j)
        {
            if(c[j] == c[k])
            {
                ++k;
                p[j] = k;
            }
            else
            {
                k = 0;
                p[j] = 0;
            }
        }
        contor = 0;
        k = 0;
        for(j = 0; j < L; ++j)
        {
            if(c[k] == sir[j])
            {
                ++k;
                if(k == l)
                {
                    ap[contor] = j - l + 1;
                    ++contor;
                    k = p[l - 1];
                }
            }
            else
            {
                if(k)
                {
                    k = p[k - 1];
                    if(k)
                    {
                        --j;
                    }
                }
            }
        }
        fout<<contor<<'\n';
        for(j = 0; j < contor; ++j)
        {
            fout<<ap[j]<<endl;
        }
    return 0;
}