vadimk772336

D

Feb 11th, 2022 (edited)
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <string>
  5.  
  6. void get_z_func(std::string s, int n, std::vector<int>& z, int shift)
  7. {
  8.  
  9.     z[0] = n - shift;
  10.     for (int i = 1, l = 0, r = 0; i < n - shift; ++i)
  11.     {
  12.         if (i <= r)
  13.             z[i] = std::min(r - i + 1, z[i - l]);
  14.  
  15.         while (i + z[i] < n && s[i + z[i] + shift] == s[z[i] + shift])
  16.             ++z[i];
  17.         if (i + z[i] - 1 > r)
  18.         {
  19.             l = i;
  20.             r = i + z[i] - 1;
  21.         }
  22.     }
  23. }
  24.  
  25.  
  26. int max_repeating_substr(std::string s)
  27. {
  28.     int n = s.length();
  29.     int k_max = 1;
  30.     for (int i = 0; i < n; ++i)
  31.     {
  32.         std::vector<int> z(n - i, 0);
  33.         get_z_func(s, n, z, i);
  34.  
  35.         for (int curr_n, k, j = 1; j < n - i; ++j)
  36.         {
  37.             curr_n = z[j] + j - ((z[j] + j) % j);
  38.  
  39.             k = curr_n / j;
  40.             if (k_max < k)
  41.                 k_max = k;
  42.         }
  43.     }
  44.  
  45.     return k_max;
  46. }
  47.  
  48. int main()
  49. {
  50.     std::string s;
  51.     std::cin >> s;
  52.     std::cout << max_repeating_substr(s);
  53.  
  54.     return 0;
  55. }
  56.  
Add Comment
Please, Sign In to add comment