Pagini recente » Cod sursa (job #1040504) | Cod sursa (job #1760906) | Cod sursa (job #2566481) | Cod sursa (job #2071327) | Cod sursa (job #1007459)
#include <fstream>
#include <cstring>
#include <map>
#define Base1 629
#define Base2 513
#define Base3 73
#define Mod1 1660019
#define Mod2 1000007
#define Mod3 3
#define Nmax 16500
using namespace std;
int n,K;
char s[Nmax];
int Pow1[Nmax],a[Mod1];
map < int,int >M;
inline bool Verif(const int k)
{
int i,Hash1 = 0,Hash2 = 0,Hash3 = 0,P1 = 1,P2 = 1,P3 = 1,Hash;
M.clear();
for(i = 1;i <= k; ++i)
{
Hash1 = (Hash1*Base1+s[i])%Mod1;
Hash2 = (Hash2*Base2+s[i])%Mod2;
Hash3 = (Hash3*Base3+s[i])%Mod3;
if(i < k)
P1 = (P1*Base1)%Mod1,
P2 = (P2*Base2)%Mod2;
P3 = (P3*Base3)%Mod3;
}
Hash = Hash1 + Hash2 + Hash3;
++M[Hash];
for(i=k+1;i<=n;++i)
{
Hash1 = ((Hash1 - (s[i - k] * P1) % Mod1 + Mod1) * Base1 + s[i]) % Mod1;
Hash2 = ((Hash2 - (s[i - k] * P2) % Mod2 + Mod2) * Base2 + s[i]) % Mod2;
Hash3 = ((Hash3 - (s[i - k] * P3) % Mod3 + Mod3) * Base3 + s[i]) % Mod3;
Hash = Hash1 + Hash2 + Hash3;
if(++M[Hash] >= K)
return true;
}
return false;
}
int main()
{
ifstream f("substr.in");
f>>n>>K>>(s+1);
f.close();
int Left = 0,Right = n,Middle, sol = 0;
while(Left<=Right)
{
Middle = (Left+Right)>>1;
if(Verif(Middle))
{
sol = Middle;
Left = Middle + 1;
}
else
Right = Middle-1;
}
ofstream g("substr.out");
g<<sol<<"\n";
g.close();
return 0;
}