Advertisement
Alexander672

genius C

Sep 4th, 2021
1,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. // #define int long long
  6. #define ft first
  7. #define sd second
  8. #define all(a) a.begin(), a.end()
  9.  
  10. typedef long long ll;
  11. typedef long double ld;
  12.  
  13. const int INF = 1e9 + 5;
  14.  
  15. void solve()
  16. {
  17.     string s;
  18.     cin >> s;
  19.     int n = (int)(s.size());
  20.     for (int i = 0; i < n; i++)
  21.     {
  22.         s.push_back(s[i]);
  23.     }
  24.     vector<int> z(2 * n, 0);
  25.     int l = 0, r = 0;
  26.     for (int i = 1; i < 2 * n; i++)
  27.     {
  28.         if (i <= r)
  29.         {
  30.             z[i] = min(r - i + 1, z[i - l]);
  31.         }
  32.         while (z[i] + i < 2 * n && s[z[i]] == s[z[i] + i])
  33.         {
  34.             z[i]++;
  35.         }
  36.         if (i + z[i] - 1 > r)
  37.         {
  38.             r = i + z[i] - 1;
  39.             l = i;
  40.         }
  41.         if (z[i] >= n)
  42.         {
  43.             cout << i;
  44.             return;
  45.         }
  46.     }
  47. }
  48.  
  49. void clear()
  50. {
  51.     cout << "\n";
  52. }
  53.  
  54. signed main()
  55. {
  56.     ios_base::sync_with_stdio(false);
  57.     cin.tie(0);
  58.     cout.tie(0);
  59.     int t = 1;
  60.     // cin >> t;
  61.     while (t--)
  62.     {
  63.         solve();
  64.         clear();
  65.     }
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement