fork download
  1. // odd[i] means palindrome starts at i - odd[i] and ends at i + odd[i]
  2. // even[i] means palindrome starts at i - even[i] - 1, and ends at i + even[i] iff even[i] != 0
  3. void manacher(const string &s, vector<int> &odd, vector<int> &even)
  4. {
  5. int n = sz(s);
  6. odd.clear();
  7. even.clear();
  8. odd.resize(n);
  9. even.resize(n);
  10. for (int i = 0, l = 0, r = -1; i < n; i++) {
  11. int k = (i > r) ? 1 : min(odd[l + r - i], r - i + 1);
  12. while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) {
  13. k++;
  14. }
  15. odd[i] = --k;
  16. if (i + k > r) {
  17. l = i - k;
  18. r = i + k;
  19. }
  20. }
  21.  
  22. for (int i = 0, l = 0, r = -1; i < n; i++) {
  23. int k = (i > r) ? 0 : min(even[l + r - i + 1], r - i + 1);
  24. while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) {
  25. k++;
  26. }
  27. even[i] = k--;
  28. if (i + k > r) {
  29. l = i - k - 1;
  30. r = i + k ;
  31. }
  32. }
  33. }
  34.  
  35. // strt[i] -> how many palindromes start at i
  36. // fnsh[i] -> how many palindromes end at i
  37. // countCover[i] -> how many palindromes cover i
  38. const int MAX_N = 2e6 + 5;
  39. long long strt[MAX_N], fnsh[MAX_N], countCover[MAX_N];
  40. void calcCover(const vector<int> &odd, const vector<int> &even)
  41. {
  42. int n = odd.size();
  43. for (int i = 0; i < n; ++i)
  44. {
  45. // odd
  46. int st = i - odd[i], en = i + odd[i];
  47. fnsh[i]++, fnsh[en + 1]--;
  48. strt[st]++, strt[i + 1]--;
  49.  
  50.  
  51. if (even[i])
  52. {
  53. st = i - even[i], en = i + even[i] - 1;
  54. fnsh[i]++, fnsh[en + 1]--;
  55. strt[st]++, strt[i]--;
  56. }
  57. }
  58.  
  59. for (int i = 1; i < n; ++i)
  60. {
  61. strt[i] += strt[i - 1];
  62. fnsh[i] += fnsh[i - 1];
  63. }
  64. long long activePalindromes = 0;
  65. for (int i = 0; i < n; ++i)
  66. {
  67. activePalindromes += strt[i];
  68. countCover[i] = activePalindromes;
  69. activePalindromes -= fnsh[i];
  70. }
  71. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:21: error: ‘string’ does not name a type; did you mean ‘struct’?
 void manacher(const string &s, vector<int> &odd, vector<int> &even)
                     ^~~~~~
                     struct
prog.cpp:3:32: error: ‘vector’ has not been declared
 void manacher(const string &s, vector<int> &odd, vector<int> &even)
                                ^~~~~~
prog.cpp:3:38: error: expected ‘,’ or ‘...’ before ‘<’ token
 void manacher(const string &s, vector<int> &odd, vector<int> &even)
                                      ^
prog.cpp: In function ‘void manacher(const int&, int)’:
prog.cpp:5:13: error: ‘sz’ was not declared in this scope
     int n = sz(s);
             ^~
prog.cpp:5:13: note: suggested alternative: ‘s’
     int n = sz(s);
             ^~
             s
prog.cpp:6:5: error: ‘odd’ was not declared in this scope
     odd.clear();
     ^~~
prog.cpp:7:5: error: ‘even’ was not declared in this scope
     even.clear();
     ^~~~
prog.cpp:11:31: error: ‘min’ was not declared in this scope
         int k = (i > r) ? 1 : min(odd[l + r - i], r - i + 1);
                               ^~~
prog.cpp:12:50: error: invalid types ‘const int[int]’ for array subscript
         while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) {
                                                  ^
prog.cpp:12:62: error: invalid types ‘const int[int]’ for array subscript
         while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) {
                                                              ^
prog.cpp:23:31: error: ‘min’ was not declared in this scope
         int k = (i > r) ? 0 : min(even[l + r - i + 1], r - i + 1);
                               ^~~
prog.cpp:24:58: error: invalid types ‘const int[int]’ for array subscript
         while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) {
                                                          ^
prog.cpp:24:70: error: invalid types ‘const int[int]’ for array subscript
         while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) {
                                                                      ^
prog.cpp: At global scope:
prog.cpp:40:22: error: ‘vector’ does not name a type
 void calcCover(const vector<int> &odd, const vector<int> &even)
                      ^~~~~~
prog.cpp:40:28: error: expected ‘,’ or ‘...’ before ‘<’ token
 void calcCover(const vector<int> &odd, const vector<int> &even)
                            ^
prog.cpp: In function ‘void calcCover(int)’:
prog.cpp:42:13: error: ‘odd’ was not declared in this scope
     int n = odd.size();
             ^~~
prog.cpp:47:25: error: ‘en’ was not declared in this scope
         fnsh[i]++, fnsh[en + 1]--;
                         ^~
prog.cpp:47:25: note: suggested alternative: ‘n’
         fnsh[i]++, fnsh[en + 1]--;
                         ^~
                         n
prog.cpp:51:13: error: ‘even’ was not declared in this scope
         if (even[i])
             ^~~~
stdout
Standard output is empty