Advertisement
IlidanBabyRage

stringt.cpp

Jun 1st, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int getGcd(int a, int b);
  6. bool checkT(string s, int t);
  7.  
  8. int main(){
  9.  
  10.     string s;
  11.     cin >> s;
  12.     int strl = s.size();
  13.     int count[26] = {};
  14.     for (int i = 0; i < strl; i++)
  15.         count[s[i] - 97]++;
  16.  
  17.     int swap;
  18.     for (int i = 1; i < 26; i++){
  19.         int j = i;
  20.         while(j > 0 && count[j] > count[j - 1]){
  21.             swap = count[j];
  22.             count[j] = count[j - 1];
  23.             count[--j] = swap;
  24.         }
  25.     }
  26.  
  27.     int gcd;
  28.     for (int i = 0; i < 26; i++){
  29.         if (count[i]){
  30.             gcd = getGcd(count[i], strl);
  31.             for (int j = gcd; j > 0; j--)
  32.                 if (!(strl % j) && checkT(s, strl / j)){
  33.                     cout << j << endl;
  34.                     return 0;
  35.                 }
  36.         }
  37.     }
  38.  
  39.     cout << 1 << endl;
  40.  
  41.     return 0;
  42. }
  43.  
  44. int getGcd(int a, int b){
  45.     if (!(a & b))
  46.         return a | b;
  47.     int shift;
  48.     for (shift = 0; !((a | b) & 1); shift++){
  49.         a >>= 1;
  50.         b >>= 1;
  51.     }
  52.     while(!(a & 1))
  53.         a >>= 1;
  54.     do{
  55.         while(!(b & 1))
  56.             b >>= 1;
  57.         if (a < b)
  58.             b -= a;
  59.         else{
  60.             int diff = a - b;
  61.             a = b;
  62.             b = diff;
  63.         }
  64.         b >>= 1;
  65.     } while(b);
  66.     return a << shift; 
  67. }
  68.  
  69. bool checkT(string s, int t){
  70.     for (int i = t; i < s.size(); i++)
  71.         if (s[i] != s[i - t])
  72.             return false;
  73.     return true;
  74. }
  75.  
  76. //aabbbccaabbbccaabbbcc
  77. //abcdefghijklmnopqrstuvwxyz
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement