Advertisement
Korotkodul

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

Jan 13th, 2022 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 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. int n,why;
  33.  
  34. vector <int> S;
  35.  
  36. string som;
  37.  
  38. vector <int> ans;
  39.  
  40. vector <int> m;
  41.  
  42. int cnt=0;
  43.  
  44. void mnk(){
  45. n = S.size();
  46. m.resize(n,0);
  47. //cout<<"S\n";
  48. //cv(S);
  49. pii lr = {-1, -1};
  50. pii now;
  51. for (int i = 0; i < n;++i){
  52. int l = lr.first, r = lr.second;
  53. if (i >= l && i <= r){
  54. int idx = l + (r - i);
  55. m[i] = min(r - i + 1, m[idx]);
  56. }
  57. while (i - m[i] >= 0 && i + m[i] < n && S[i - m[i]] == S[i + m[i]]){
  58. ++m[i];
  59. }
  60. now = {i - m[i] + 1, i + m[i] - 1};
  61. if (now.second > lr.second){
  62. lr = now;
  63. }
  64. }
  65. for (int i = 0;i<som.size();++i){
  66. //cout<<som[i]<<" "<<'#'<<' ';
  67. }
  68. //cout<<"\n";
  69. //cout<<"S m\n";
  70. //cv(S);
  71. //cv(m);
  72. for (int i = 0; i < n;++i){
  73. if (m[i] != 0){
  74. m[i]--;
  75. }
  76. cnt += m[i] / 2;
  77. }
  78. }
  79.  
  80. int main()
  81. {
  82. ios::sync_with_stdio(0);
  83. cin.tie(0);
  84. cout.tie(0);
  85. //cin>>n>>why;
  86. /*S = {0};
  87. for (int i=0;i<n;++i) {
  88. int x;
  89. cin>>x;
  90. S.push_back(x);
  91. S.push_back(0);
  92. }
  93. mnk();
  94. sort(ans.begin(), ans.end());
  95. //cout<<"ans\n";
  96. cv(ans);*/
  97. //int x = 'a' - 96;
  98. //cout<<x<<"\n";
  99. cin>>som;
  100. S = {0};
  101. for (int i = 0;i<som.size();++i){
  102. int d = som[i] - 96;
  103. // cout<<"i = "<<i<<"\n";
  104. //cout<<"som[i] = "<<som[i]<<"\n";
  105. //cout<<"d = "<<d<<"\n\n";
  106. S.push_back(d);
  107. S.push_back(0);
  108. }
  109. //cout<<"S\n";
  110. //cv(S);
  111. mnk();
  112. cout<<cnt;
  113. }
  114. /*
  115. 6 2
  116. 1 1 2 2 1 1
  117.  
  118. abcbaceedeeakaee
  119.  
  120. */
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement