Cod sursa(job #2762845)

Utilizator AndreiDeltaBalanici Andrei Daniel AndreiDelta Data 9 iulie 2021 17:34:49
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
typedef long long ll;
typedef pair<int,int> pi;
int t,T;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string P,T;
    f>>P;
    f>>T;
    int np=P.size();
    int nt=T.size();
    P=" "+P;
    T=" "+T;
    vector < int > dpP(np+1,0),dpT(nt+1,0);
    dpP[1]=0;
    for(int i=2;i<=np;i++)
    {
        int k=dpP[i-1]; //cel mai lung prefix inpropriu a lui P ce este si sufin pe pozitia i a lui P
        while(k>0 && P[i]!=P[k+1]) k=dpP[k];

        dpP[i]=k+(P[i]==P[k+1]);
    }

    dpT[0]=0;
    int ans=0;
    vector < int > v;
    for(int i=1;i<=nt;i++)
    {
        int k=dpT[i-1];
        while(k>0 && T[i]!=P[k+1]) k=dpP[k];

        dpT[i]=k+(T[i]==P[k+1]);

        if(dpT[i]==np)
        {
            ans++;
            if(ans<=1000)
                v.pb(i-np);
        }
    }
    g<<ans<<'\n';
    for(int x: v) g<<x<<' ';

    return 0;
}