Cod sursa(job #631372)

Utilizator sunt_emoSunt emo sunt_emo Data 7 noiembrie 2011 21:30:28
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>
#include <cstring>
#include <iostream>
#define N 2000010
using namespace std;

std::ifstream in ("strmatch.in");
std::ofstream out ("strmatch.out");
char s[N],w[N];
long pmt[N],i,k,c[N],l,n,m;

void ff (char *a) {
    pmt[0]=-1;
    for (i=1; a[i-1]; i++) if (a[i-1]==a[pmt[i-1]]) pmt[i]=pmt[i-1]+1;
}

void kmp (char *s,char *w) {
    k=0;
    while (k+m<=n) {
        i=0;
        while (s[k]==w[i]&&k<n) {
            k++;
            i++;
            if (i==m) c[l++]=k-i;
        }
        k-=pmt[i];
    }
}

int main () {
    in.getline (w,N);
    in.getline (s,N);
    n=strlen (s);
    m=strlen (w);
    ff (w);
    kmp (s,w);
    out<<l<<"\n";
    for (i=0; i<l; i++) out<<c[i]<<" ";
    out<<"\n";
    return 0;
}