Cod sursa(job #2883966)

Utilizator valentinchipuc123Valentin Chipuc valentinchipuc123 Data 2 aprilie 2022 01:27:05
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <bits/stdc++.h>

using namespace std;

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

int N,M,pi[2000005];
string A,B;

void automate()
{
    int i,poz=0;
    pi[1]=0;

    for(int i=2; i<=M; i++)
    {
        while(poz&&A[i]!=A[poz+1]) poz=pi[poz];
        if(A[poz+1]==A[i]) poz++;
        pi[i]=poz;
    }
}

int main()
{
    f>>A>>B;
    M=A.size();
    N=B.size();
    A=" "+A;
    B=" "+B;

    automate();

    int poz=0;
    vector<int>elem;

    for(int i=1; i<=N; i++)
    {
        while( poz&&A[poz+1]!=B[i] ) poz=pi[poz];
        if(A[poz+1]==B[i]) poz++;

        if(poz==M)
        {
            poz=pi[poz];
            if(elem.size()<1000) elem.push_back(i-M);
        }
    }

    g<<elem.size()<<'\n';
    for(int i=0; i<elem.size(); i++) g<<elem[i]<<' ';

    return 0;
}