Pagini recente » Cod sursa (job #351421) | Cod sursa (job #328664) | Cod sursa (job #3200769) | Cod sursa (job #1796976) | Cod sursa (job #1052568)
#include <cstdio>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <unordered_map>
#define B 123
#define Nmax 17000
using namespace std;
char buf[Nmax];
int N,K,sol = 1, sol_max =-1;
int run(int sz)
{
unordered_map<unsigned int, int> hash;
string x = "";
unsigned key = 0,power = 1;
for(int i=0;i<sz;++i)
{
if(i!=0)
power *= B;
key = (key*B + buf[i]);
}
hash[key] = 1;
int maxim=-1;
for(int i=sz;i<N;++i)
{
key = (key - power * buf[i-sz])*B + buf[i];
if(hash.find(key) != hash.end())
hash[key] = hash[key] + 1;
else
hash[key] = 1;
if(hash[key] > maxim)
maxim = hash[key];
}
return maxim;
}
int main()
{
ifstream f("substr.in");
f>>N>>K;
f.getline(buf,Nmax);
f.getline(buf,Nmax);
f.close();
int left,right,mid;
left = 0;right = N-1;
while(left<=right)
{
mid = (left+right)/2;
int x = run(mid);
if(x >= K)
{
if(mid > sol)
sol = mid;
left = mid+1;
}
else
{
right = mid-1;
}
}
ofstream g("substr.out");
g<<sol<<"\n";
g.close();
return 0;
}