Cod sursa(job #2216065)

Utilizator TeddyDinutaDinuta Eduard Stefan TeddyDinuta Data 24 iunie 2018 23:24:54
Problema Potrivirea sirurilor Scor 38
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream in("strmatch.in");
ofstream out("strmatch.out");
char a[2000100],b[2000100],x;
int i,n,m,k,j,nr,v[2000100],q;
int p[2000100];
int main()
{
    in.get(a,2000100);
    n=strlen(a);
    for(i=n+1;i>0;i--)
        a[i]=a[i-1];
    while(in>>x)
        b[++k]=x;
    j=1;
    i=2;
    p[1]=0;
    while(i<=n)
    {
       while(a[i]!=a[j]&&j>1) j=p[j-1];
       if(a[i]==a[j])
       {
           p[i]=j;
           i++;
           j++;
       }
       else p[i]=0,i++;
    }
    j=0;
    i=1;
    while(i<=k)
    {
        while(b[i]!=a[j+1]&&j>0) j=p[j];
        if(b[i]==a[j+1])
        {
            i++;
            j++;
        }
        else i++;
        if(j==n)
        {
            v[++q]=i-n-1;
            i=i-n+1;
        }
    }
    out<<q<<'\n';
    for(i=1;i<=min(q,1000);i++) out<<v[i]<<" ";
    return 0;
}