Pagini recente » Borderou de evaluare (job #1053583) | Cod sursa (job #1058581)
#include <fstream>
#include <vector>
#include <bitset>
#include <tr1/unordered_map>
#include <map>
using namespace std;
using namespace tr1;
const char infile[] = "substr.in";
const char outfile[] = "substr.out";
ifstream cin(infile);
ofstream cout(outfile);
const int MAXN = 17000;
const int oo = 0x3f3f3f3f;
const int MOD = 16385000;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
int N, K;
char S[MAXN];
unordered_map <int, int> Hash;
inline bool Check(int x) {
Hash.clear();
int last = 1, _hash = 0;
for(int i = 0 ; i < x ; ++ i) {
_hash = ( (_hash << 5) + _hash + S[i]) % MOD;
last = ( (last << 5) + last) % MOD;
}
++ Hash[_hash];
for(int i = x ; i < N ; ++ i) {
_hash = ((_hash << 5) + _hash + S[i]) % MOD;
_hash -= ( last * S[i - x] ) % MOD;
if( _hash < 0 )
_hash += MOD;
if(++ Hash[_hash] >= K)
return 1;
}
return 0;
}
int main() {
cin >> N >> K >> S;
int st = 0, dr = N, Ans = N;
if(K == 1)
goto _end;
while(st <= dr) {
int mid = ((st + dr) >> 1);
if(Check(mid)) {
Ans = mid;
st = mid + 1;
}
else dr = mid - 1;
}
_end:
cout << Ans << '\n';
cin.close();
cout.close();
return 0;
}