Pagini recente » Cod sursa (job #2083195) | Cod sursa (job #2087950) | Cod sursa (job #858104) | Cod sursa (job #2904068) | Cod sursa (job #1832557)
#include <fstream>
#include <string>
#include <vector>
using namespace std;
ifstream fin("substr.in");
ofstream fout("substr.out");
const int sigma= 26+26+10;
const int mod1= 666013;
const int mod2= 99997;
int n, k;
int cnt[mod1];
string s;
struct str {
int x, y;
};
vector <str> v[mod1];
inline str mp( int x, int y ) {
str sol;
sol.x= x, sol.y= y;
return sol;
}
int value( char x ) {
if ( x>='0' && x<='9' ) {
return x-'0';
} else if ( x>='a' && x<='z' ) {
return 10+x-'a';
}
return 10+26+x-'A';
}
bool check( int x ) {
int p1= 1, p2= 1;
for ( int i= 1; i<=x-1; ++i, p1= (p1*sigma)%mod1, p2= (p2*sigma)%mod2 ) ;
int h1= 0, h2= 0;
for ( int i= 1; i<=x; ++i ) {
h1= (h1*sigma+value(s[i-1]))%mod1;
h2= (h2*sigma+value(s[i-1]))%mod2;
}
cnt[h1]= x;
v[h1].clear();
v[h1].push_back(mp(h2, 1));
if ( 1>=k ) {
return 1;
}
for ( int i= x+1; i<=n; ++i ) {
h1= (((h1-p1*value(s[i-1-x]))*sigma+value(s[i-1]))%mod1+mod1)%mod1;
h2= (((h2-p2*value(s[i-1-x]))*sigma+value(s[i-1]))%mod2+mod2)%mod2;
if ( cnt[h1]!=x ) {
v[h1].clear();
cnt[h1]= x;
}
int ok= 0;
for ( int j= 0; j<(int)v[h1].size() && ok==0; ++j ) {
if ( v[h1][j].x==h2 ) {
++v[h1][j].y;
ok= 1;
if ( v[h1][j].y>=k ) {
return 1;
}
}
}
if ( ok==0 ) {
v[h1].push_back(mp(h2, 1));
}
}
return 0;
}
int main( ) {
fin>>n>>k>>s;
int sol= 0, step;
for ( step= 1; step*2<=n; step*= 2 ) ;
for ( ; step>0; step/= 2 ) {
if ( sol+step<=n && check(sol+step) ) {
sol+= step;
}
}
fout<<sol<<"\n";
return 0;
}