Pagini recente » Cod sursa (job #1713000) | Cod sursa (job #941196) | Cod sursa (job #1033697) | Cod sursa (job #398941) | Cod sursa (job #1047648)
#include <fstream>
#include <unordered_map>
using namespace std;
ifstream cin("substr.in");
ofstream cout("substr.out");
const int mod = int(1e9) + 7;
const int mod_ = 666013;
int N, K;
string str;
int h[16385], h_[16385];
int INV[16385], INV_[16385];
unordered_map<int,int> H, H_;
inline int getIndex(char x) {
return x - 'a' + 1;
}
int logpow(int a,int b,const int &m) {
int ret = 1;
for (;b > 0;b >>= 1, a = 1LL * a * a % m) {
if (b & 1) {
ret = 1LL * ret * a % m;
}
}
return ret;
}
void compute() {
int X = 26;
int X_ = 26;
h[1] = getIndex(str[0]);
h_[1] = getIndex(str[0]);
INV[0] = 1;
INV_[0] = 1;
for (int i = 2;i <= N;i++,X = 1LL * X * 26 % mod, X_ = 1LL * X_ * 26 % mod_) {
h[i] = (1LL * h[i - 1] + 1LL * X * getIndex(str[i - 1])) % mod;
h_[i] = (1LL * h_[i - 1] + 1LL * X_ * getIndex(str[i - 1])) % mod_;
INV[i - 1] = logpow(X,mod - 2,mod);
INV_[i - 1] = logpow(X_,mod_ - 2,mod_);
}
}
inline int getHash(int l,int r) {
return 1LL * (h[r] - h[l - 1] + mod) * INV[l - 1] % mod;
}
inline int getHash_(int l,int r) {
return 1LL * (h_[r] - h_[l - 1] + mod_) * INV_[l - 1] % mod_;
}
bool isGood(int L) {
int hash, hash_;
H.clear();
H_.clear();
for (int i = 1;i + L - 1 <= N;i++) {
hash = getHash(i,i + L - 1);
hash_ = getHash_(i,i + L - 1);
H[hash]++;
H_[hash_]++;
if(H[hash] >= K && H_[hash_] >= K) {
return true;
}
}
return false;
}
int main()
{
cin >> N >> K;
cin.get();
getline(cin,str);
compute();
int ans = 0;
for (int step = 1 << 15;step > 0;step >>= 1) {
if(step + ans <= N && isGood(step + ans)) {
ans += step;
}
}
cout << ans;
return 0;
}