Pagini recente » Cod sursa (job #3181209) | Cod sursa (job #2603453) | Cod sursa (job #2728694) | Cod sursa (job #880373) | Cod sursa (job #2238674)
#include <fstream>
#include <cstring>
#include <unordered_map>
using namespace std;
ifstream fin ("substr.in");
ofstream fout ("substr.out");
const int mod1 = 1013, mod2 = 1013,base1 = 19,Dim = 170385,base2 = 29;
const int NMAX = (1 << 14);
const int MOD = 1000000007;
const int P = 26;
int Hash[mod1][mod2],n,k,Powbase1[Dim],Powbase2[Dim];
char s[1 + NMAX];
int hash1[1 + NMAX];
int put[1 + NMAX];
char T[Dim];
bool Check(int lung);
int main() {
fin >> n >> k >> ( T + 1 );
put[0] = 1;
for(int i = 1; i <= n; i++) {
put[i] = 1LL * put[i - 1] * P % MOD;
hash1[i] = (1LL * hash1[i - 1] * P + T[i] - 'a') % MOD;
}
int st = 1, dr = n / k + 1,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 lg) {
int sol = 1;
unordered_map <int, int> f;
f[hash1[lg]]++;
for(int i = lg + 1; i <= n; i++) {
int h = (hash1[i] - (1LL * hash1[i - lg] * put[lg]) % MOD + MOD) % MOD;
f[h]++;
}
for(auto it : f)
sol = max(sol, it.second);
return (sol >= k);
}