vadimk772336

С принята

Feb 10th, 2022 (edited)
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. void get_z_func(std::string s, int n, std::vector<int>& z)
  7. {
  8.  
  9.     z[0] = n;
  10.     for (int i = 1, l = 0, r = 0; i < n; ++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]] == s[z[i]])
  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. int get_period(std::string s, int n)
  26. {
  27.     std::vector<int> z(n, 0);
  28.     get_z_func(s, n, z);
  29.  
  30.     for (int i = 1; i < n; ++i)
  31.     {
  32.         if ((n % i == 0) && (z[i] + i == n))
  33.             return (n / i);
  34.     }
  35.  
  36.     return 1;
  37. }
  38.  
  39. int main()
  40. {
  41.     std::string s;
  42.     std::cin >> s;
  43.     int n = s.length();
  44.  
  45.     int period = get_period(s, n);
  46.  
  47.     std::cout << period;
  48.  
  49.     return 0;
  50. }
  51.  
Add Comment
Please, Sign In to add comment