Cod sursa(job #2445892)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 5 august 2019 21:19:16
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <fstream>
#include <cstring>
#include <vector>

using namespace std;

ifstream f("strmatch.in");
ofstream g("strmatch.out");

const int NMAX = 2e6 + 5;

char A[2 * NMAX], B[NMAX];

int N, M, T, auxN, phi[2 * NMAX], ans;

vector < int > Sol;

int main()
{
    f.tie(NULL);
    f >> A >> B;

    N = (int)strlen(A);
    auxN = N;
    M = (int)strlen(B);

    strcat(A, "#");
    strcat(A, B);

    T = (int)strlen(A);

    ans = 0;

    for(int i = 1; i < T; ++i)
    {
        while(ans && A[i] != A[ans])
            ans = phi[ans - 1];

        if(A[i] == A[ans])
            ++ans;

        phi[i] = ans;
    }

    for(int i = auxN + 1; i < T; ++i)
        if(phi[i] == auxN)
            Sol.push_back(i - 2 * auxN);

    g << (int)Sol.size() << '\n';

    for(int i = 0; i < min(1000, (int)Sol.size()); ++i)
        g << Sol[i] << ' ';

    g << '\n';

    return 0;
}