SuitNdtie

LongestRepeatSubStringMap

May 10th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<map>
  3. using namespace std;
  4. typedef long long int ll;
  5. ll const Pr = 98765431;
  6.  
  7. ll mpow(ll a,ll b){
  8.     ll ta = a;
  9.     if(b == 0)return 1;
  10.     for(int i = 1 ; i < b ; i ++){
  11.         ta *= a;
  12.     }
  13.     return ta;
  14. }
  15.  
  16. int main()
  17. {
  18.     int n,m;
  19.     scanf("%d %d",&n,&m);
  20.     char str[n+1];
  21.     scanf("%s",str);
  22.     int L = 1 , R = n;
  23.     int ans = 0;
  24.     while(L <= R){
  25.         int size = (L+R)/2;
  26.         map<ll,int> table;table.clear();
  27.         ll mp = mpow(Pr,size-1);
  28.         ll hs = 0;
  29.         ll maxRe = 0;
  30.     //  printf("Test size : %d ",size);
  31.         for(int i = 0 ; i < n && maxRe < m ; i ++){
  32.             hs *= Pr;
  33.             hs += str[i];
  34.             if(i >= size - 1){
  35.                 if(table.count(hs)){ //Repeat
  36.                     table[hs]++;
  37.                 }
  38.                 else{ //new
  39.                     table.insert({hs,1});
  40.                 }
  41.                 if(table[hs] > maxRe)maxRe = table[hs];
  42.                 hs -= mp*str[i-size+1];
  43.             }
  44.         }
  45.     //  printf(", maxRe = %d\n",maxRe);
  46.         if(maxRe >= m){
  47.             ans = size;
  48.             L = size + 1;
  49.         }
  50.         else{
  51.             R = size - 1;
  52.         }
  53.     }
  54.     printf("%d",ans);
  55.     return 0;
  56. }
Add Comment
Please, Sign In to add comment