Pagini recente » Cod sursa (job #154052) | Cod sursa (job #2923043) | Cod sursa (job #101078) | Cod sursa (job #1807769) | Cod sursa (job #1069070)
#include <fstream>
#include <map>
using namespace std;
ifstream in("substr.in");
ofstream out("substr.out");
const int NMAX = 16384;
const int BASE = 137;
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 = 0; i < value; i++){
hashKey = hashKey*BASE + data[i];
basePower *= BASE;
}
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;
}
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;
}*/
for(result = 1; result <= N && isMatching(result); result++)
;
out << result -1 << '\n';
return 0;
}