Cod sursa(job #2198250)

Utilizator RaduVFVintila Radu-Florian RaduVF Data 23 aprilie 2018 23:27:51
Problema Potrivirea sirurilor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <cstring>
using namespace std;

ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
const int nmax=2e6+3;
int n,m,c,pi[nmax],pos[1024];
char a[nmax],b[nmax];
void make_prefix() {
    int q=0;
    for(int i=2;i<=m;++i) {
        while(q&&a[q+1]!=a[i]) q=pi[q];
        if(a[q+1]==a[i]) ++q;
        pi[i]=q;
    }
}
int main()
{
    int q=0;
    fin.getline(a+1,nmax);
    fin.getline(b+1,nmax);
    m=strlen(a+1);
    n=strlen(b+1);
    make_prefix();
    for(int i=1; i<=n; ++i) {
        while(q&&a[q+1]!=b[i]) q=pi[q];
        if(a[q+1]==b[i]) ++q;
        if(q==m) {
            q=pi[m];
            ++c;
            if(c<=1000) pos[c]=i-m;
        }
    }
    fout<<c<<endl;
    for(int i=1; i<=min(c,1000); ++i) fout<<pos[i]<<' ';
    return 0;
}