Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- void get_z_func(std::string s, int n, std::vector<int>& z)
- {
- z[0] = n;
- for (int i = 1, l = 0, r = 0; i < n; ++i)
- {
- if (i <= r)
- z[i] = std::min(r - i + 1, z[i - l]);
- while (i + z[i] < n && s[i + z[i]] == s[z[i]])
- ++z[i];
- if (i + z[i] - 1 > r)
- {
- l = i;
- r = i + z[i] - 1;
- }
- }
- }
- int get_period(std::string s, int n)
- {
- std::vector<int> z(n, 0);
- get_z_func(s, n, z);
- for (int i = 1; i < n; ++i)
- {
- if ((n % i == 0) && (z[i] + i == n))
- return (n / i);
- }
- return 1;
- }
- int main()
- {
- std::string s;
- std::cin >> s;
- int n = s.length();
- int period = get_period(s, n);
- std::cout << period;
- return 0;
- }
Add Comment
Please, Sign In to add comment