Cod sursa(job #2792338)

Utilizator andrei_C1Andrei Chertes andrei_C1 Data 1 noiembrie 2021 14:45:22
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.27 kb
#include <bits/stdc++.h>

std :: ifstream fin("strmatch.in");
std :: ofstream fout("strmatch.out");

const int DIM = 2e6;
const int P1 = 67;
const int P2 = 61;
const int MOD1 = 1e9 + 7;
const int MOD2 = 1e9 + 9;

int N, M, cnt;
std :: string a, b;
int value[256];
std :: vector<int>ans;
int hash_a1, hash_a2, hash_b1, hash_b2;

//inline int value(char a) {
//    if(isupper(a)) {
//        return a - 'A';
//    }
//    if(islower(a)) {
//        return a - 'a' + 26;
//    }
//    if(isdigit(a)) {
//        return a - '0' + 26 + 26;
//    }
//    return 0;
//}

int main() {
    fin >> a >> b;

    N = a.size();
    M = b.size();

    if(N > M) {
        fout << "0\n";
        return 0;
    }

    for(int i = 'A'; i <= 'Z'; i++) {
        value[i] = i - 'A';
    }
    for(int i = 'a'; i <= 'z'; i++) {
        value[i] = i - 'a' + 26;
    }
    for(int i = '0'; i <= '9'; i++) {
        value[i] = i - '0' + 52;
    }

    int p1 = 1, p2 = 1;
    for(int i = 0; i < N; i++) {
        hash_a1 = ((long long)P1 * hash_a1 % MOD1 + value[a[i]]) % MOD1;
        hash_a2 = ((long long)P2 * hash_a2 % MOD2 + value[a[i]]) % MOD2;

        if(i != N - 1) {
            p1 = (long long)p1 * P1 % MOD1;
            p2 = (long long)p2 * P2 % MOD2;
        }
    }

    for(int i = 0; i < N; i++) {
        hash_b1 = ((long long)P1 * hash_b1 % MOD1 + value[b[i]]) % MOD1;
        hash_b2 = ((long long)P2 * hash_b2 % MOD2 + value[b[i]]) % MOD2;
    }

    if(hash_a1 == hash_b1 && hash_a2 == hash_b2) {
        cnt = 1;
        ans.push_back(0);
    }

    for(int i = N; i < M; i++) {
        hash_b1 = ((long long)hash_b1 - (long long)value[b[i - N]] * p1 % MOD1 + MOD1) % MOD1;
        hash_b2 = ((long long)hash_b2 - (long long)value[b[i - N]] * p2 % MOD2 + MOD2) % MOD2;

        hash_b1 = ((long long)hash_b1 * P1 % MOD1 + value[b[i]]) % MOD1;
        hash_b2 = ((long long)hash_b2 * P2 % MOD2 + value[b[i]]) % MOD2;

        if(hash_a1 == hash_b1 && hash_a2 == hash_b2) {
            if(++cnt <= 1000) {
                ans.push_back(i - N + 1);
            }
        }
    }

    fout << cnt << '\n';
    for(int i = 0; i < ans.size(); i++) {
        fout << ans[i] << " ";
    }

    fin.close();
    fout.close();
    return 0;
}