Cod sursa(job #2878260)

Utilizator Ana100Ana-Maria Tomoiala Ana100 Data 26 martie 2022 12:09:10
Problema Potrivirea sirurilor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.61 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream cin("strmatch.in");
ofstream cout("strmatch.out");
const int Dim = 2e6 + 5;
const int base = 29;
const int mod = 66667;
char a[Dim], b[Dim];
int lengthA,lengthB;
int hashA, hashB;
int nrSol, power = 1;
int solutions[1001];


void Precaluate() {

    for (int i  = 1; i <= lengthA-1; ++i)
        power = (1LL * power * base) % mod;
}

void CreateHashA() {

    for ( int i = 1; i <= lengthA; ++i)
        hashA = ((1LL * hashA * base) % mod + a[i] - 'A') % mod;
    cout << hashA << "\n";

}

void FindMatches() {

    // Luam primele lengthA cractere
    for( int i = 1; i <= lengthA; ++ i) {
        hashB = ((1LL * hashB * base) % mod + b[i] - 'A') % mod;
    }
    if(hashB == hashA) {
        nrSol++;
        if(nrSol <= 1000)
            solutions[nrSol] = 1;
    }


    for ( int i = lengthA+1; i <= lengthB; ++i) {
        //scoatem prima litera din hashB
        hashB = (hashB - ((1LL * (b[i-lengthA]-'A') * power) % mod)   + mod) % mod;
        hashB = ((1LL * hashB * base) % mod + b[i] - 'A') % mod;
        cout << hashB << "\n";
         if(hashB == hashA) {
        nrSol++;
        if(nrSol <= 1000)
            solutions[nrSol] = i-lengthA+1;
    }
    }

}

void Print() {
    cout << nrSol << "\n";
    for ( int i = 1; i <= min(1000,nrSol); ++i)
        cout << solutions[i] << " ";

}
int main()  {

    cin >> (a+1) >> (b+1);

    lengthB = strlen(b+1);
    cout << lengthB << "\n";
    lengthA = strlen(a+1);

    Precaluate();
    CreateHashA();
    FindMatches();
    Print();

}