Korotkodul

ЗОШ хэши K5 debug

Jan 13th, 2022 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define vec vector
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17. for (auto x: v) cout<<x<<' ';
  18. cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22. for (auto x: v) cout<<x<<' ';
  23. cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28. for (auto x: v) cv(x);
  29. cout<<"\n";
  30. }
  31. string S;
  32.  
  33. int n;
  34.  
  35. vector <ll> pf, lvp;
  36.  
  37. const int p = 29, mod = 1e9 + 7;
  38.  
  39.  
  40. int nm(char x){
  41. return x - 'a' + 1;
  42. }
  43.  
  44. ll H(int l, int r){
  45. ll k = r - l + 1;
  46. ll res = pf[r];
  47. if (l > 0){
  48. res -= pf[l-1] * lvp[k] % mod;
  49. }
  50. if (res < 0) res += mod;
  51. return res;
  52. }
  53.  
  54.  
  55.  
  56. int l1, r1, l2, r2;
  57.  
  58. int cmp(){
  59. int lf, rt, md;
  60. //бинописком ищем длину наибольшего общего префикса
  61. lf = 0;
  62. rt = min(r2 - l2, r1 - l1);
  63. //cout<<"lf rt = "<<lf<<' '<<rt<<"\n";
  64. //cout<<"l r = "<<l1<<' '<<r1<<' '<<l2<<' '<<r2<<"\n";
  65. while (rt - lf > 1){
  66. md = (lf + rt) / 2;
  67. ll a,b;
  68. a = H(l1, l1 + md);
  69. b = H(l2, l2 + md);
  70. //cout<<"md = "<<md<<"\n";
  71. //cout<<"a b = "<<a<<' '<<b<<"\n";
  72. if (a == b){
  73. lf = md;
  74. }
  75. else rt= md;
  76. }
  77. //cout<<"rt = "<<rt<<"\n";
  78. rt--;
  79. if (rt<0)rt=0;
  80. while (S[l1+rt]==S[l2+rt] && l1 + rt <= r1 && l2 + rt <= r2) rt++;
  81. //cout<<"rt = "<<rt<<"\n";
  82. if (l1 + rt > r1 || l2 + rt > r2){
  83. return 0;
  84. }
  85. if (S[l1 + rt] > S[l2 + rt]){
  86. //cout<<"four\n";
  87. return 1;
  88. }
  89. if (S[l1 + rt] < S[l2 + rt]){
  90. //cout<<"five\n";
  91. return -1;
  92. }
  93. if (S[l1 + rt] == S[l2 + rt]){
  94. //cout<<"six\n";
  95. return 0;
  96. }
  97. }
  98.  
  99. string rndS(){
  100. string res = "";
  101. int sz = rand() % 100 + 20;
  102. string alp = "abcdefghijklmnopqrstuvwxyz";
  103. for (int i = 0;i < sz;++i){
  104. int id = rand() % alp.size();
  105. res += alp[id];
  106. }
  107. return res;
  108. }
  109.  
  110. int stp(){
  111. string a,b;
  112. a = S.substr(l1, r1 - l1 + 1);
  113. b = S.substr(l2, r2 - l2 + 1);
  114. if (a > b){
  115. return 1;
  116. }
  117. if (a < b){
  118. return -1;
  119. }
  120. return 0;
  121. }
  122.  
  123. void cpr(pii x){
  124. cout<<x.first<<' '<<x.second<<"\n";
  125. }
  126.  
  127.  
  128. int main()
  129. {
  130. //ios::sync_with_stdio(0);
  131. //cin.tie(0);
  132. //cout.tie(0);
  133. //cin>>S;
  134. S = rndS();
  135. n = S.size();
  136. lvp.assign(n+5, 1);
  137. for (int i = 1; i<=n;++i) lvp[i] = lvp[i-1] * p % mod;
  138. pf.assign(n, nm(S[0]));
  139. for (int i =1;i<n;++i){
  140. pf[i] = (pf[i-1] * p % mod + nm(S[i])) % mod;
  141. }
  142. //int q; cin>>q;
  143. int ans;
  144. cout<<S<<"\n";
  145. vector <vector<int>> lr;
  146. vector <pii> checker;
  147. while (1){
  148. //cin>>l1>>r1>>l2>>r2;
  149. l1 = rand() % S.size();
  150. r1 = l1 + rand() % (n - l1);
  151. if (r1 < l1) r1=l1;
  152. if (r1 >= n) r1=n-1;
  153. l2 = rand() % S.size();
  154. r2 = l2 + rand() % (n - l2);
  155. if (r2 < l2) r2=l2;
  156. if (r2>=n) r2=n-1;
  157. ans = cmp();
  158. int chk = stp();
  159. //cout<<l1<<' '<<r1<<' '<<l2<<' '<<r2<<"\n";
  160. //cout<<"ans = "<<ans<<"\n";
  161. //cout<<ans<<"\n";
  162. if (ans != chk){
  163. //cout<<l1<<' '<<r1<<' '<<l2<<' '<<r2<<"\n";
  164. //cout<<"ans= "<<ans<<"\n";
  165. //cout<<"chk= "<<chk<<"\n";
  166. vector <int> v = {l1, r1, l2, r2};
  167. lr.push_back(v);
  168. checker.push_back({ans, chk});
  169. }
  170. if (checker.size() >= 10) break;
  171. }
  172. cout<<"CHECKING DONE\n";
  173. for (int i=0;i<checker.size();++i){
  174. cv(lr[i]);
  175. cpr(checker[i]);
  176. cout<<"\n";
  177. }
  178. }
  179. /*
  180. hqghumeaylnlfdxfircvscxggbwkfnqduxwfnfozvsrtkjprepggxrpnrvyst
  181. CHECKING DONE
  182. 58 59 58 60
  183. 0 -1
  184.  
  185. 17 59 56 56
  186. 0 1
  187.  
  188. 4 8 4 25
  189. 0 -1
  190.  
  191. 19 52 19 19
  192. 0 1
  193.  
  194. 40 44 40 42
  195. 0 1
  196.  
  197. 23 59 23 46
  198. 0 1
  199.  
  200. 23 46 51 51
  201. 0 1
  202.  
  203. 35 47 35 51
  204. 0 -1
  205.  
  206. 10 39 29 29
  207. 0 1
  208.  
  209. 15 32 12 12
  210. 0 1
  211. */
  212.  
Add Comment
Please, Sign In to add comment