Pagini recente » Cod sursa (job #1338106) | Cod sursa (job #79048) | Cod sursa (job #35613) | Cod sursa (job #2499303) | Cod sursa (job #2563456)
#include <fstream>
#include <algorithm>
#include <unordered_map>
#define mod 666013
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
int n,K,st,i,dr,mij,p1[16390],p2[16390],h1,h2,sol,nr,mx,t,k;
struct per{
int x,y;
}v[16390];
char s[16390];
int caracter(char c){
if(c>='a' && c<='z')
return c-'a'+1;
if(c>='A' && c<='Z')
return c-'A'+27;
return c-'0'+53;
}
int cmp(per a, per b){
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
int main(){
fin>>n>>K>>(s+1);
p1[0]=p2[0]=1;
for(i=1;i<=n;i++){
p1[i]=(1ll*p1[i-1]*67)%mod;
p2[i]=(1ll*p2[i-1]*73)%mod;
}
st=1,dr=n; sol=0;
while(st<=dr){
mij=(st+dr)/2;
h1=h2=0;
for(i=1;i<=mij;i++){
int x=caracter(s[i]);
h1=(1ll*h1*67+x)%mod;
h2=(1ll*h2*73+x)%mod;
}
v[++t]={h1,h2};
for(i=mij+1;i<=n;i++){
int x=caracter(s[i]);
int c=caracter(s[i-mij]);
h1=((1ll*h1-1ll*c*p1[mij-1])*67+x)%mod;
if(h1<0)
h1+=mod;
h2=((1ll*h2-1ll*c*p2[mij-1])*73+x)%mod;
if(h2<0)
h2+=mod;
v[++t]={h1,h2};
}
sort(v+1,v+t+1,cmp);
mx=1; bool ok=0; int ct=1;
int x=v[1].x,y=v[1].y;
for(i=2;i<=t;i++){
if(x==v[i].x && y==v[i].y){
ct++;
mx=max(mx,ct);
ok=0;
}
else{
x=v[i].x;
y=v[i].y;
ct=1;
}
}
t=0;
if(mx>=K){
sol=mij;
st=mij+1;
}
else
dr=mij-1;
}
fout<<sol;
return 0;
}