fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. string a;
  31.  
  32. bool isPossible(int n, int mid){
  33.  
  34. if (mid == 0) return true;
  35.  
  36. int P = 31;
  37. int MOD = 1e9 + 7;
  38. int p_pow = 1;
  39.  
  40. for (int i = 0; i < mid - 1; i++) {
  41. p_pow = (p_pow * P) % MOD;
  42. }
  43.  
  44. int s = 0;
  45. int e = 0;
  46. int hash = 0;
  47.  
  48. //* substring -> index
  49. unordered_map<int,int> mp;
  50.  
  51. while(e < n){
  52. hash = (hash * P + (a[e] - 'a' + 1)) % MOD;
  53.  
  54. if(e - s + 1 < mid){
  55. e++;
  56. }
  57. else{
  58. if(mp.count(hash)){
  59. if(s >= mp[hash] + mid) return true;
  60. }
  61. else{
  62. mp[hash] = s;
  63. }
  64.  
  65.  
  66. hash = (hash - (a[s] - 'a' + 1) * p_pow) % MOD;
  67. if (hash < 0) hash += MOD;
  68.  
  69. e++;
  70. s++;
  71. }
  72. }
  73. return false;
  74. }
  75.  
  76.  
  77. int consistency(int n){
  78.  
  79. int s = 0;
  80. int e = n/2+1;
  81.  
  82.  
  83. while(s<e){
  84. int mid = s + (e-s+1)/2;
  85.  
  86. if(isPossible(n, mid)){
  87. s = mid;
  88. }
  89. else{
  90. e = mid-1;
  91. }
  92.  
  93. }
  94.  
  95.  
  96.  
  97. return e;
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. void solve() {
  119.  
  120. cin >> a;
  121. int n = a.size();
  122.  
  123. cout << consistency(n) << endl;
  124.  
  125.  
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. int32_t main() {
  133. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  134.  
  135. int t = 1;
  136. // cin >> t;
  137. while (t--) {
  138. solve();
  139. }
  140.  
  141. return 0;
  142. }
Success #stdin #stdout 0s 5316KB
stdin
abacdabaweacda
stdout
4