fork 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> first;
  50. unordered_map<int,int> second;
  51.  
  52. while(e < n){
  53. hash = (hash * P + (a[e] - 'a' + 1)) % MOD;
  54.  
  55. if(e - s + 1 < mid){
  56. e++;
  57. }
  58. else{
  59. if(!first.count(hash)){
  60. first[hash] = s;
  61. }
  62. else if(!second.count(hash)){
  63. if(s >= first[hash] + mid) second[hash] = s;
  64. }
  65. else{
  66. if(s >= second[hash] + mid) return true;
  67. }
  68.  
  69.  
  70. hash = (hash - (a[s] - 'a' + 1) * p_pow) % MOD;
  71. if (hash < 0) hash += MOD;
  72.  
  73. e++;
  74. s++;
  75. }
  76. }
  77. return false;
  78. }
  79.  
  80.  
  81. int consistency(int n){
  82.  
  83. int s = 0;
  84. int e = n/3+1;
  85.  
  86.  
  87. while(s<e){
  88. int mid = s + (e-s+1)/2;
  89.  
  90. if(isPossible(n, mid)){
  91. s = mid;
  92. }
  93. else{
  94. e = mid-1;
  95. }
  96.  
  97. }
  98.  
  99.  
  100.  
  101. return e;
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. void solve() {
  123.  
  124. cin >> a;
  125. int n = a.size();
  126.  
  127. cout << consistency(n) << endl;
  128.  
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136. int32_t main() {
  137. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  138.  
  139. int t = 1;
  140. // cin >> t;
  141. while (t--) {
  142. solve();
  143. }
  144.  
  145. return 0;
  146. }
Success #stdin #stdout 0s 5316KB
stdin
abacdabaweacdaaba
stdout
3