Cod sursa(job #1290016)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 10 decembrie 2014 18:31:00
Problema Potrivirea sirurilor Scor 14
Compilator cpp Status done
Runda Arhiva educationala Marime 2.63 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;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#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 // HOME

typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;

const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = (int)(1e9) + 7;

const int NMAX = 50000 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 2000000 + 5;
const int VMAX = 100000 + 5;
const int BASE = 'z' + 1;
const int MOD1 = 1e5 + 9;
const int MOD2 = 1e5 + 7;

int LA, LB;
char A[LMAX];
char B[LMAX];
int nrMatches;
int matches[1005];

void rolling_hash() {
    int i, hash1, hash2, B1, B2, H1, H2;

    hash1 = hash2 = 0;
    B1 = B2 = 1;

    for(i = 1; i <= LA; i++) {
        hash1 = (hash1 * 1LL * BASE + A[i]) % MOD1;
        hash2 = (hash2 * 1LL * BASE + A[i]) % MOD2;
        if(i >= 2) {
            B1 = (B1 * 1LL * BASE) % MOD1;
            B2 = (B2 * 1LL * BASE) % MOD2;
        }
    }

    H1 = H2 = 0;

    for(i = 1; i <= LA - 1; i++) {
        H1 = (H1 * 1LL * BASE + B[i]) % MOD1;
        H2 = (H2 * 1LL * BASE + B[i]) % MOD2;
    }

    for(; i <= LB; i++) {
        H1 = (((H1 - B[i - LA] * 1LL * B1 + MOD1) % MOD1) * 1LL * BASE + B[i]) % MOD1;
        H2 = (((H2 - B[i - LA] * 1LL * B2 + MOD2) % MOD2) * 1LL * BASE + B[i]) % MOD2;
        if(H1 == hash1 && H2 == hash2) {
            nrMatches++;
            if(nrMatches <= 1000)
                matches[nrMatches] = i - LA;
        }
    }
}

int main() {
    int i;

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

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

    LA = strlen(A + 1);
    LB = strlen(B + 1);

    rolling_hash();

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

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

    return 0;
}