Pagini recente » Cod sursa (job #3163338) | Cod sursa (job #2259766) | Cod sursa (job #3221892) | Cod sursa (job #1303468) | Cod sursa (job #1052557)
#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 MOD1 100007
#define MOD2 100021
#define BASE 123
#define Nmax 17000
using namespace std;
char buf[Nmax];
int N,K,sol = -1, sol_max =-1;
int run(int sz)
{
unordered_map<int, int> hash1;
unordered_map<int, int> hash2;
string x = "";
int key1,key2,power1, power2;
key1 = key2 = 0;
power1 = power2 = 1;
for(int i=0;i<sz;++i)
{
if(i!=0)
{
power1 = (power1*BASE)%MOD1;
power2 = (power2*BASE)%MOD2;
}
key1 = (key1*BASE + buf[i])%MOD1;
key2 = (key2*BASE + buf[i])%MOD2;
}
hash1[key1] = 1;
hash2[key2] = 1;
int maxim=-1;
for(int i=sz;i<N;++i)
{
key1 = (((key1 - power1 * buf[i-sz])%MOD1 + MOD1)*BASE + buf[i])%MOD1;
key2 = (((key2 - power2 * buf[i-sz])%MOD2 + MOD2)*BASE + buf[i])%MOD2;
if(hash1.find(key1) != hash1.end() && hash2.find(key2) != hash2.end()) {
hash1[key1] = hash1[key1] + 1;
hash2[key2] = hash2[key2] + 1;
}
else
hash1[key1] = hash2[key2] = 1;
if(hash1[key1] > maxim)
maxim = hash1[key1];
}
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;
}