Cod sursa(job #2238562)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 6 septembrie 2018 12:17:54
Problema Substr Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <fstream>
#include <cstring>
#include <cmath>
 
using namespace std;
 
ifstream fin ("substr.in");
ofstream fout ("substr.out");
 
const int mod1 = 1013, mod2 = 2013,base1 = 1079,Dim = 19385,base2 = 26;
int Hash[mod1][mod2],n,k,Powbase1[Dim],Powbase2[Dim];
char T[Dim];
bool Check(int lung);
int main() {
     
    fin >> n >> k >> ( T + 1 );
    Powbase1[0] = 1;
    for ( int i = 1; i <= n; ++i)
        Powbase1[i] = (1LL * Powbase1[i-1] * base1) % mod1;
    Powbase2[0] = 1;
    for ( int i = 1; i <= n; ++i)
        Powbase2[i] = (1LL * Powbase2[i-1] * base2) % mod2;
     
    int st = 1, dr = n,mj,sol = 0;
    while ( st <= dr) {
        mj = ( st + dr ) / 2;
        if ( Check(mj) ) 
            st = mj + 1, sol = mj;
        else
            dr = mj - 1;
    }
    fout << sol;
}
 
bool Check(int lung) {
     
    memset(Hash,0,sizeof(Hash));
    int cHash = 0,CHash = 0;
        for ( int j = 1; j <= n; ++j) {
            cHash = (cHash * base1+ (abs(T[j] -'a') + 3)) % mod1;
            CHash = (CHash * base2+ (abs(T[j] -'a') + 3)) % mod2;
                if ( j > lung) {
                cHash = ((cHash - (( abs(T[j-lung] - 'a') + 3)* Powbase1[lung] % mod1)) % mod1 + mod1) % mod1;
                CHash = (CHash - ((abs(T[j-lung] - 'a') + 3)* Powbase2[lung]) % mod2 + mod2) % mod2;
                  }
            if ( j >= lung) {
               
                Hash[cHash][CHash]++;
				    if (Hash[cHash][CHash] >= k)
						return true;
				}
        }
   
    return false;
 }