Pagini recente » Cod sursa (job #2769280) | Cod sursa (job #959038) | Cod sursa (job #3276233) | Cod sursa (job #3236150) | Cod sursa (job #2314122)
# include <fstream>
# include <string>
# include <map>
# define DIM 16484
# define PRAG 10000000
#define MOD1 100007
#define MOD2 100021
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
char ch[DIM];
map<long long,int> m;
int n,k,step,i,j,h1,h2,p=73,p1,p2,r1,r2;
int el(char ch){
if(ch>='0'&&ch<='9')
return 53+ch-'0';
if(ch>='a'&&ch<='z')
return ch-'a'+1;
if(ch>='A'&&ch<='Z')
return ch-'A'+27;
}
bool val(int q){
p1=p2=1;
for(i=1;i<=q;i++){
h1=(p*h1+el(ch[i]))%MOD1;
h2=(p*h2+el(ch[i]))%MOD2;
if(i!=1){
p1*=p;
p2*=p;
p1%=MOD1;
p2%=MOD2;
}
}
if(!m.count(1LL*h1*PRAG+h2))
m[1LL*h1*PRAG+h2]=1;
else
m[1LL*h1*PRAG+h2]++;
if(m[1LL*h1*PRAG+h2]==k){
m.clear();
h1=h2=0;
p1=p2=1;
return 1;
}
for(i=q+1;i<=n;i++){
r1=p1*el(ch[i-q])%MOD1;
r2=p2*el(ch[i-q])%MOD2;
h1-=r1;
if(h1<0)
h1+=MOD1;
h2-=r2;
if(h2<0)
h2+=MOD2;
h1=(h1*p+el(ch[i]))%MOD1;
h2=(h2*p+el(ch[i]))%MOD2;
if(!m.count(1LL*h1*PRAG+h2))
m[1LL*h1*PRAG+h2]=1;
else
m[1LL*h1*PRAG+h2]++;
if(m[1LL*h1*PRAG+h2]==k){
m.clear();
h1=h2=0;
p1=p2=1;
return 1;
}
}
h1=h2=0;
p1=p2=1;
m.clear();
return 0;
}
int sol(){
for(step=1;step<=n;step*=2);
for(j=0;step;step/=2)
if(j+step<=n&&val(j+step))
j+=step;
return j;
}
int main () {
fin>>n>>k>>ch+1;
fout<<sol();
return 0;
}