Advertisement
aayyk

Untitled

Mar 22nd, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <stack>
  8. #include <climits>
  9. #include <string>
  10. #include <set>
  11. #include <cmath>
  12. #include <map>
  13. #include <unordered_map>
  14. #include <numeric>
  15. #include <random>
  16. #include <memory>
  17. #include <chrono>
  18. #include <iterator>
  19. #include <functional>
  20. #include <unordered_set>
  21. #include <cassert>
  22. #include <cstring>
  23. #include <bitset>
  24. #ifdef LOCAL
  25. #include "debug.h"
  26. #else
  27. #define debug(x...)
  28. #endif
  29. //#define int ll
  30.  
  31. //#pragma GCC optimize("O3")
  32. //#pragma GCC target("avx2")
  33.  
  34. using namespace std;
  35. typedef long long ll;
  36. typedef long double ld;
  37. typedef pair < int, int > pii;
  38. typedef pair < ll, ll > pll;
  39. #define sz(x) int((x).size())
  40.  
  41. #ifndef LOCAL
  42. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  43. #else
  44. mt19937 rng(228);
  45. #endif
  46.  
  47. const int N = 5e6 + 7;
  48. const int inf = INT_MAX / 2;
  49. const ll INF = LLONG_MAX / 3;
  50. const int MOD = 1e9 + 7;
  51. const double eps = 1e-9;
  52. const string cars[] = {"🚗", "🚕", "🚙"};
  53.  
  54. int c[N], ans[N];
  55. int n;
  56.  
  57. bool check(int len) {
  58.     for (int i = len - 1; i < n; i += len) {
  59.         if (!c[i]) {
  60.             return false;
  61.         }
  62.     }
  63.  
  64.     return true;
  65. }
  66.  
  67. bool cmp(ld a, ld b) {
  68.     return abs(a - b) < eps;
  69. }
  70.  
  71. int solve1(string& s) {
  72.     n = sz(s);
  73.     int a[27] = { 0 };
  74.  
  75.     memset(ans, 0, sizeof ans);
  76.     memset(c, 0, sizeof c);
  77.  
  78.     for (char c : s) {
  79.         a[c - 'a']++;
  80.     }
  81.  
  82.     int cur[27] = { 0 };
  83.     for (int i = 0; i < sz(s); i++) {
  84.         cur[s[i] - 'a']++;
  85.  
  86.         int flag = 1;
  87.         ld v = -1;
  88.         for (int j = 0; j < 26 && flag; j++) {
  89.             if (cur[j]) {
  90.                 if (v == -1) {
  91.                     v = ld(a[j]) / cur[j];
  92.                 }
  93.                 else {
  94.                     flag &= cmp(v, ld(a[j]) / cur[j]);
  95.                 }
  96.             }
  97.             else {
  98.                 flag &= !a[j];
  99.             }
  100.         }
  101.  
  102.         if (flag) {
  103.             c[i] = 1;
  104.             memset(cur, 0, sizeof cur);
  105.         }
  106.  
  107.         //debug(i, flag);
  108.     }
  109.  
  110.     for (int i = 1; i * i <= n; i++) {
  111.         if (n % i == 0) {
  112.             ans[i] |= check(i);
  113.             ans[n / i] |= check(n / i);
  114.         }
  115.     }
  116.  
  117.     for (int i = 1; i <= n; i++) {
  118.         if (ans[i]) {
  119.             return n / i;
  120.         }
  121.         //cout << i << " " << ans[i] << endl;
  122.     }
  123.     return 1;
  124. }
  125.  
  126. int solve2(string& s) {
  127.     for (int ans = 1; ans <= sz(s); ans++) {
  128.         if (sz(s) % ans) {
  129.             continue;
  130.         }
  131.  
  132.         vector < string > a;
  133.         for (int i = 0; i < sz(s); i += ans) {
  134.             string t = s.substr(i, ans);
  135.             sort(t.begin(), t.end());
  136.            
  137.             a.push_back(t);
  138.         }
  139.         sort(a.begin(), a.end());
  140.  
  141.         if (a.front() == a.back()) {
  142.             return sz(s) / ans;
  143.         }
  144.     }
  145. }
  146.  
  147. int rng_on(int l, int r) {
  148.     return rng() % (r - l + 1) + l;
  149. }
  150.  
  151. signed main() {
  152.     //debug(true);
  153. #ifdef LOCAL
  154.     freopen("input.txt", "r", stdin);
  155.     //freopen("output.txt", "w", stdout);
  156. #endif
  157.     cout << fixed << setprecision(4);
  158.     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  159.  
  160.     string s;
  161.     cin >> s;
  162.    
  163.     cout << solve1(s) << endl;
  164.     return 0;
  165.  
  166.  
  167.     for (int it = 0; it < 1e5; it++) {
  168.         int n = rng_on(1, 20);
  169.         string s;
  170.         for (int i = 0; i < n; i++) {
  171.             s += 'a' + rng_on(0, 1);
  172.         }
  173.  
  174.         if (solve1(s) != solve2(s)) {
  175.             debug(n, s);
  176.             debug(solve1(s), solve2(s));
  177.  
  178.             return 0;
  179.         }
  180.     }
  181.  
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement