Cod sursa(job #1981668)

Utilizator MaligMamaliga cu smantana Malig Data 16 mai 2017 13:50:12
Problema Potrivirea sirurilor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 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 ui unsigned int
#define pb push_back
const int strMax = 2e6 + 5;
const int base = 73;

int N,M,nrSol;
char patt[strMax],str[strMax];
int sol[strMax];

int main() {
    in>>(patt+1)>>(str+1);

    N = strlen(patt+1);
    M = strlen(str+1);

    ui ph = 0, sh = 0,pw = 1;

    for (int i=1;i <= N;++i) {
        ph = ph * base + patt[i] * base;
        sh = sh * base + str[i] * base;

        pw *= base;
    }

    if (ph == sh) {
        sol[++nrSol] = 0;
    }

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

        if (ph == sh) {
            sol[++nrSol] = i-N;
        }
    }

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

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