Advertisement
Korotkodul

ЗОШ Монакер полиндромы стресс-тест

Jan 13th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 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.  
  32. void cvc(vector <char> v){
  33. for (char x: v) cout<<x<<' ';
  34. cout<<"\n";
  35. }
  36.  
  37. vector <char> S;
  38. int n;
  39. vector <int> m;
  40. vector <int> clr;
  41. //clear m for clc pol-s
  42.  
  43. int cnt=0;
  44. string som;
  45.  
  46.  
  47.  
  48.  
  49.  
  50. void mnk(){
  51. //cout<<"S\n";
  52. //cvc(S);
  53. n = S.size();
  54. m.resize(n, 0);
  55. clr.resize(n);
  56. pii lr = {-1, -1}, now;
  57. for (int i = 0;i <n;++i){
  58. int l = lr.first, r = lr.second;
  59. if (i >= l && i <= r){
  60. int idx = l + (r - i);
  61. m[i] = min(r - i + 1, m[idx]);
  62. }
  63. while (i - m[i] >= 0 && i + m[i] < n && S[i - m[i]] == S[i + m[i]]){
  64. ++m[i];
  65. }
  66. now = {i - m[i] + 1, i + m[i] - 1};
  67. if (now.second > lr.second){
  68. lr = now;
  69. }
  70. }
  71. for (int i=0;i<n;++i){
  72. if (S[i] != '@') m[i]--;
  73. clr[i] = m[i] / 2;
  74. }
  75. for (int i = 0;i<n;++i) {
  76. cnt += clr[i];
  77. }
  78. }
  79. int check=0;
  80. void stp(){//stupid
  81. som = '@' + som + '@';
  82. n = som.size();
  83. //cout<<"som n = "<<som<<' '<<n<<"\n";
  84. //cout<<"check = "<<check<<"\n";
  85. //сначала считаем четные полиндромы
  86. int id1 , id2 ;
  87. for (int i = 1; i < n-1; ++i){
  88. id1 = i - 1;
  89. id2 = i + 1;
  90. //cout<<"ONE\n";
  91. while (id1 >= 0 && id2 < n && som[id1] == som[id2]){
  92. //cout<<"id1 id2 = "<<id1<<' '<<id2<<"\n";
  93. if (som[id1] == '@' || som[id2] == '@') break;
  94. //cout<<"id1 id2 = "<<id1<<' '<<id2<<"\n";
  95.  
  96. id1--;
  97. id2++;
  98. check++;
  99. //cout<<"+\n";
  100. }
  101. id1 = i;
  102. id2 = i + 1;
  103. //cout<<"TWO\n";
  104. while (id1 >=0 && id2 < n && som[id1] == som[id2]){
  105. if (som[id1] == '@' || som[id2] == '@') break;
  106. //cout<<"id1 id2 = "<<id1<<' '<<id2<<"\n";
  107. id1--;
  108. id2++;
  109. check++;
  110. //cout<<"+\n";
  111. }
  112. }
  113. //потом нечетные
  114. }
  115.  
  116. string generateTheString(int n)
  117. {
  118. string ans="";
  119. // If n is odd
  120. if(n%2)
  121. {
  122. // Add all characters from
  123. // b-y
  124. for(int i=0;i<min(n,24);i++)
  125. {
  126. ans+=(char)('b' + i);
  127. }
  128. // Append a to fill the
  129. // remaining length
  130. if(n>24)
  131. {
  132. for(int i=0;i<(n-24);i++)
  133. ans+='a';
  134. }
  135. }
  136. // If n is even
  137. else
  138. {
  139. // Add all characters from
  140. // b-z
  141. for(int i=0;i<min(n,25);i++)
  142. {
  143. ans+=(char)('b' + i);
  144. }
  145. // Append a to fill the
  146. // remaining length
  147. if(n>25)
  148. {
  149. for(int i=0;i<(n-25);i++)
  150. ans+='a';
  151. }
  152. }
  153.  
  154. return ans;
  155. }
  156.  
  157. #include <stdio.h> /* printf, scanf, puts, NULL */
  158. #include <stdlib.h> /* srand, rand */
  159. #include <time.h>
  160.  
  161. int main()
  162. {
  163. ios::sync_with_stdio(0);
  164. cin.tie(0);
  165. cout.tie(0);
  166. //cin>>som;
  167. do {
  168. int sz = rand() % 100;
  169. cout<<"sz = "<<sz<<"\n";
  170. cout<<som<<"\n";
  171. som = generateTheString(sz);
  172. S = {'@'};
  173. for (int i=0;i<som.size();++i){
  174. S.push_back(som[i]);
  175. S.push_back('@');
  176. }
  177. stp();
  178. mnk();
  179. } while (check == cnt);
  180. }
  181. /*
  182. aaaa
  183.  
  184. cdeedcfcbcf
  185.  
  186. abcdeedcfbfaartl
  187.  
  188. ltlmlm
  189.  
  190. kakaka
  191. */
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement