Cod sursa(job #1183649)

Utilizator mateidanutDanut Gabriel Matei mateidanut Data 9 mai 2014 22:05:13
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#include <string.h>
using namespace std;

const int q=101, d=256;
int n, m, i, j, p, t, h=1;
char a[2000003], b[2000003];
int poz[2000003], k;

ifstream f("strmatch.in");
ofstream g("strmatch.out");

int main()
{
    f.getline(b, 2000003);
    f.get(a, 2000003);
    n=strlen(a);
    m=strlen(b);
    for (i=1; i<m; ++i)
        h=(h*d)%q;
    for (i=0; i<m; ++i) {
        p=(p*d+b[i])%q;
        t=(t*d+a[i])%q;
    }
    for (i=0; i<=n-m; ++i) {
        if (p==t) {
            for (j=0; j<m; ++j)
                if (a[i+j]!=b[j])
                    j=m+1;
            if (j==m)
                poz[++k]=i;
        }
        if (i<n-m) {
            t=(d*(t-h*a[i])+a[i+m])%q;
            if (t<0)
                t+=q;
        }
    }
    g<<k<<'\n';
    for (i=1; i<=k; ++i)
        g<<poz[i]<<' ';
    return 0;
}