Pagini recente » Cod sursa (job #2769279) | Cod sursa (job #3275126) | Cod sursa (job #2998414) | Cod sursa (job #2526336) | Cod sursa (job #2315674)
# include <fstream>
# include <string>
# include <vector>
# 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];
int n,k,step,i,j,h1,h2,p=73,p1,p2,r1,r2,val;
vector<long long> Hash[MOD1];
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;
int nr;
int ok=0;
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;
}
}
val=(1LL*h1*PRAG+h2)%MOD1;
Hash[val].push_back(1LL*h1*PRAG+h2);
nr=0;
for(int j=0;j<Hash[val].size();j++)
if(Hash[val][j]==1LL*h1*PRAG+h2)
nr++;
if(nr>=k){
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;
val=(1LL*h1*PRAG+h2)%MOD1;
Hash[val].push_back(1LL*h1*PRAG+h2);
nr=0;
for(int j=0;j<Hash[val].size();j++)
if(Hash[val][j]==1LL*h1*PRAG+h2)
nr++;
if(nr>=k){
h1=h2=0;
p1=p2=1;
return 1;
}
}
h1=h2=0;
p1=p2=1;
return 0;
}
int sol(){
for(step=1;step<=n-k+1;step*=2);
for(j=0;step;step/=2)
if(j+step<=n-k+1&&Val(j+step))
j+=step;
return j;
}
int main () {
fin>>n>>k>>ch+1;
fout<<sol();
return 0;
}