Pagini recente » Cod sursa (job #3288569) | Cod sursa (job #186965) | Cod sursa (job #102531) | Cod sursa (job #3171231) | Cod sursa (job #1069079)
#include <fstream>
#include <map>
using namespace std;
ifstream in("substr.in");
ofstream out("substr.out");
const int NMAX = 16384;
const int BASE = 123;
string data;
map<unsigned int, int> hashMap;
int N, K;
bool isMatching(int value){
unsigned int hashKey = 0, basePower = 1, i, v, result = 0; //dispersion by overflow
for(i = 1, hashKey = data[0]; i < value; i++){
hashKey = hashKey*BASE + data[i];
basePower *= BASE;
}
hashMap[hashKey] = 1;
for(i = value; i < N; i++){
hashKey = (hashKey - data[i-value] * basePower) * BASE + data[i];
v = ++hashMap[hashKey];
if(v > result)
result = v;
}
hashMap.clear();
if(result >= K)
return true;
else return false;
}
int main()
{
in >> N >> K >> data;
int step = 1, result ;
while(step <= N) step<<=1;
for(result = 0; step; step>>=1){
hashMap.clear();
if(result + step <= N && isMatching(result+step))
result+=step;
}
out << result << '\n';
return 0;
}