Pagini recente » Cod sursa (job #2549493) | Cod sursa (job #347279) | Cod sursa (job #400663) | Cod sursa (job #365121) | Cod sursa (job #3209799)
#include <bits/stdc++.h>
#define P 123457
#define Q 777013
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
/**
'A' - 'Z' = 0 - 25
'a' - 'z' = 26 - 51
'0' - '9' = 52 - 62
*/
int Cifra(char ch)
{
if('A' <= ch and ch <= 'Z')return ch - 'A';
if('a' <= ch and ch <= 'z')return 26 + (ch - 'a');
return 52 + (ch - '0');
}
string a;
int n, m, k;
vector<pair<int, int>>sol;
int Cauta(int q)
{
int p, p1, x1, x2, x3, x4, i, l = 1, lgmax = 1;
p = p1 = 1;
x1 = x2 = x3 = x4 = 0;
for(i = 1; i < q; i++)
{
p = (p * 62) % P;
p1 = (p1 * 62) % Q;
}
for(i = 0; i < q; i++)
{
x1 = (x1 * 62 + (Cifra(a[i]))) % P;
x2 = (x2 * 62 + (Cifra(a[i]))) % Q;
}
sol.push_back({x1, x2});
for(i = q; i < n; i++)
{
x1 = (x1 - Cifra(a[i - q]) * p % P + P) % P;
x1 = (x1 * 62 + Cifra(a[i])) % P;
x2 = (x2 - Cifra(a[i - q]) * p1 % Q + Q) % Q;
x2 = (x2 * 62 + Cifra(a[i])) % Q;
sol.push_back({x1, x2});
}
sort(sol.begin(), sol.end());
for(i = 0; i < sol.size(); i++)
{
if(sol[i] == sol[i - 1])
{
l++;
lgmax = max(lgmax, l);
}
else l = 1;
}
if(lgmax >= k)return 1;
return 0;
}
int main()
{
int st, dr, poz = -1, mij;
ios_base::sync_with_stdio(0);
fin.tie(0);
fout.tie(0);
fin >> n >> k;
fin >> a;
st = 1;
dr = n;
while(st <= dr)
{
mij = (st + dr) / 2;
if(Cauta(mij) == 1)
{
poz = mij;
st = mij + 1;
}
else dr = mij - 1;
}
fout << poz;
return 0;
}