Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int getGcd(int a, int b);
- bool checkT(string s, int t);
- int main(){
- string s;
- cin >> s;
- int strl = s.size();
- int count[26] = {};
- for (int i = 0; i < strl; i++)
- count[s[i] - 97]++;
- int swap;
- for (int i = 1; i < 26; i++){
- int j = i;
- while(j > 0 && count[j] > count[j - 1]){
- swap = count[j];
- count[j] = count[j - 1];
- count[--j] = swap;
- }
- }
- int gcd;
- for (int i = 0; i < 26; i++){
- if (count[i]){
- gcd = getGcd(count[i], strl);
- for (int j = gcd; j > 0; j--)
- if (!(strl % j) && checkT(s, strl / j)){
- cout << j << endl;
- return 0;
- }
- }
- }
- cout << 1 << endl;
- return 0;
- }
- int getGcd(int a, int b){
- if (!(a & b))
- return a | b;
- int shift;
- for (shift = 0; !((a | b) & 1); shift++){
- a >>= 1;
- b >>= 1;
- }
- while(!(a & 1))
- a >>= 1;
- do{
- while(!(b & 1))
- b >>= 1;
- if (a < b)
- b -= a;
- else{
- int diff = a - b;
- a = b;
- b = diff;
- }
- b >>= 1;
- } while(b);
- return a << shift;
- }
- bool checkT(string s, int t){
- for (int i = t; i < s.size(); i++)
- if (s[i] != s[i - t])
- return false;
- return true;
- }
- //aabbbccaabbbccaabbbcc
- //abcdefghijklmnopqrstuvwxyz
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement