Cod sursa(job #1409311)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 30 martie 2015 14:38:11
Problema Potrivirea sirurilor Scor 14
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "strmatch";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif

const int NMAX = 2000000 + 5;
const int BASE1 = 127;
const int BASE2 = 256;
const int MOD1 = 666013;
const int MOD2 = 100007;

char A[NMAX];
char B[NMAX];
int N, M;
int H1, H2, B1, B2;
int nrP, P[1005];

int main() {
    int i;
    int h1, h2;

#ifndef ONLINE_JUDGE
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);
#endif

    scanf("%s", A + 1);
    scanf("%s", B + 1);

    N = strlen(A + 1);
    M = strlen(B + 1);

    B1 = B2 = 1;

    for(i = 1; i <= N; i++) {
        H1 = (H1 * 1LL *  BASE1 + A[i]) % MOD1;
        H2 = (H2 * 1LL *  BASE2 + A[i]) % MOD2;
        B1 = (B1 * 1LL *  BASE1) % MOD1;
        B2 = (B2 * 1LL *  BASE2) % MOD2;
    }

    for(i = 1; i <= M; i++) {
        h1 = (h1 * 1LL * BASE1 + B[i]) % MOD1;
        h2 = (h2 * 1LL * BASE2 + B[i]) % MOD2;
        if(i >= N) h1 = (h1 - (B1 * 1LL *  B[i - N]) % MOD1 + MOD1) % MOD1;
        if(i >= N) h2 = (h2 - (B2 * 1LL *  B[i - N]) % MOD2 + MOD2) % MOD2;

        if(h1 == H1 && h2 == H2) {
            nrP++;
            if(nrP <= 1000)
                P[nrP] = i - N;
        }
    }

    printf("%d\n", nrP);

    for(i = 1; i <= min(1000, nrP); i++)
        printf("%d ", P[i]);

    return 0;
}