Advertisement
matheus_monteiro

Power Strings

Nov 5th, 2023
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2.  
  3. /*
  4.  * Author: Matheus Monteiro
  5.  */
  6.  
  7. using namespace std;
  8.  
  9. // #ifdef DEBUGMONTEIRO
  10. #define _ << " , " <<
  11. #define bug(x) cout << #x << "  >>>>>>>  " << x << endl;
  12. // #else
  13. // #define bug(x)
  14. // #endif
  15.  
  16. // #define int long long
  17. #define Max(a, b) (a > b ? a : b)
  18. #define Min(a, b) (a < b ? a : b)
  19. #define ii pair<int, int>
  20. #define f first
  21. #define s second
  22. #define vi vector<int>
  23. #define vii vector<ii>
  24. #define LSOne(S) ((S) & (-S))
  25. #define UNTIL(t) while (clock() < (t) * CLOCKS_PER_SEC)
  26. #define SZ(a) (int)a.size()
  27. #define fastio std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);
  28.  
  29. const int MAX = 200005;
  30. // const int MOD = 1000000007; //10^9 + 7
  31. const int OO = 0x3f3f3f3f; // 0x3f3f3f3f;
  32. const double EPS = 1e-9;  //10^-9
  33. std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
  34.  
  35. vector<int> getZ(const string &s) {
  36.   vector<int> z(s.size());
  37.   int l = 0, r = 0;
  38.  
  39.   for(int i = 1; i < s.size(); ++i) {
  40.     if(i < r) {
  41.       z[i] = min(r - i, z[i - l]);
  42.     }
  43.     while(i + z[i] < s.size() && s[z[i]] == s[i + z[i]])
  44.       z[i]++;
  45.     if(i + z[i] > r) {
  46.       l = i;
  47.       r = i + z[i];
  48.     }
  49.   }
  50.   return z;
  51. }
  52.  
  53. void solve() {
  54.   string s;
  55.   while(cin >> s && s != ".") {
  56.     auto z = getZ(s);
  57.     int ans = 1;
  58.     for(int i = 1; i < s.size(); ++i)
  59.       if(i + z[i] >= s.size() && (s.size() % i == 0)) {
  60.         ans = s.size() / i;
  61.         break;
  62.       }
  63.     cout << ans << '\n';
  64.   }
  65. }
  66.  
  67. int32_t main() {
  68.     fastio;
  69.  
  70.     int t = 1;
  71.    // std::cin >> t;
  72.     for(int caso = 1; caso <= t; ++caso) {
  73.         //cout << "Case #" << caso << ": ";
  74.         solve();
  75.     }
  76.  
  77.     return 0;
  78. }
  79.  
  80. /*
  81. Before submit:
  82.     Check the corners cases
  83.     Check solution restrictions
  84.  
  85. For implementation solutions:
  86.     Check the flow of the variables
  87.  
  88. For intervals problems:
  89.     Think about two pointers
  90.  
  91. For complete search:
  92.     Think about cuts
  93.  
  94. If you get WA:
  95.     Reread your code looking for stupid typos
  96.     Try various manual testcases
  97.     Recheck the correctness of your algorithm
  98.     Reread the statement
  99.     Write a straightforward solution, create lots of testcases by yourself, and compare both solutions
  100.     Generate random test cases and perform a stress test, comparing your solution with the one that you are sure is correct
  101.     Change the coder (if you're with a team)
  102.     Give up. You may have other tasks to solve.
  103. */
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement