Advertisement
DontCallMeNuttoPleas

LRS

Jul 17th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. using lli=long long;
  4. int n,m;
  5. const lli pb=1e9+7;
  6. string c;
  7.  
  8. bool check(int len){
  9.     map<lli,lli> mp;
  10.     lli hsh=0;
  11.     lli base=1;
  12.     for(int i=0;i<len;i++){
  13.         hsh*=pb;
  14.         hsh+=c[i]-'a';
  15.         if(i!=0) base*=pb;
  16.     }
  17.     mp.insert(make_pair(hsh,1));
  18.     for(int i=len;i<n;i++){
  19.         hsh-=(c[i-len]-'a')*base;
  20.         hsh*=pb;
  21.         hsh+=c[i]-'a';
  22.         if(mp.find(hsh)==mp.end()){
  23.             mp.insert(make_pair(hsh,1));
  24.         }else{
  25.             mp[hsh]++;
  26.             if(mp[hsh]>=m){
  27.                 return true;
  28.             }
  29.         }
  30.     }
  31.     return false;
  32. }
  33.  
  34. int main(){
  35.     scanf("%d%d",&n,&m);
  36.     cin >> c;
  37.     int l=1,r=n,ans=1;
  38.     while(l<=r){
  39.         int mid=(l+r)/2;
  40.         if(check(mid)){
  41.             ans=max(ans,mid);
  42.             l=mid+1;
  43.         }else{
  44.             r=mid-1;
  45.         }
  46.     }
  47.     printf("%d",ans);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement