Cod sursa(job #2556065)

Utilizator ivddabDabelea Ioana-Viviana ivddab Data 24 februarie 2020 17:39:25
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>
#define NM 2000002
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
int n,m,l;
int nr[NM],urm[NM];
string p,t;
void prefix(int n,string p){
  int i,j;
  urm[0]=0; j=-1;
  for(i=1;i<n;i++){
    while(j>0&&p[j+1]!=p[i]) j=urm[j];
    if(p[j+1]==p[i]) j++;
    urm[i]=j;
  }
}
int main()
{
    f>>p>>t;
    n=p.size(); m=t.size();
    prefix(n,p);
    int i,j;
    j=-1;
    for(i=0;i<m&&l<1000;i++){
        while(j>0&&p[j+1]!=t[i]) j=urm[j];
        if(p[j+1]==t[i]) j++;
        if(j==n-1&&l<1000){ nr[++l]=i-n+1;
         // j=urm[j];
        }
    }
    g<<l<<'\n';
    for(i=1;i<=l;i++)
        g<<nr[i]<<' ';
    return 0;
}