Cod sursa(job #1992315)

Utilizator MaligMamaliga cu smantana Malig Data 20 iunie 2017 08:19:35
Problema Potrivirea sirurilor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;
ifstream in("strmatch.in");
ofstream out("strmatch.out");

#define ll long long
#define pb push_back
#define ui unsigned int
const int inf = 1e9 + 5;
const int NMax = 2e6 + 5;
const int base = 73;

int N,M;
int sol[NMax];
char str[NMax],patt[NMax];

int main() {
    in>>(patt+1)>>(str+1);
    N = strlen(patt+1);
    M = strlen(str+1);

    ui pattHash = 0,strHash = 0,pw1 = 1,pw2 = 1;
    for (int i=1;i <= N;++i) {
        pattHash = pattHash * base + patt[i] * base;
        strHash = strHash * base + str[i] * base;

        pw1 *= base;
        pw2 *= base;
    }

    if (pattHash == strHash) {
        sol[++sol[0]] = 0;
    }

    for (int i=N+1;i <= M;++i) {
        strHash = (strHash - pw1 * str[i-N]) * base + str[i] * base;

        if (pattHash == strHash) {
            sol[++sol[0]] = i-N;
        }
    }

    out<<sol[0]<<'\n';
    sol[0] = min(sol[0],1000);
    for (int i=1;i <= sol[0];++i) {
        out<<sol[i]<<' ';
    }

    in.close();out.close();
    return 0;
}