1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
   | struct Manacher {                    static const int maxn = 1e6 + 666;     char ma[maxn << 1];     int mp[maxn << 1], begat[maxn];
      void build(char *str) {         int len = strlen(str + 1), l = 0;         ma[l++] = '$';         ma[l++] = '#';         for (int i = 1; i <= len; i++) {             ma[l++] = str[i];             ma[l++] = '#';         }         ma[l] = mp[l] = 0;         int mx = 0, id = 0;         for (int i = 0; i < l; i++) {             mp[i] = mx > i ? min(mp[2 * id - i], mx - i) : 1;             while (ma[i + mp[i]] == ma[i - mp[i]])mp[i]++;             if (i + mp[i] > mx) {                 mx = i + mp[i];                 id = i;             }         }         
                   for (int i = 1; i <= len; i++)begat[i] = 0;         for (int i = 2; i < l; i++) {             int s = i - mp[i] + 1;             s = (s + 1) / 2;             int t = s + mp[i] / 2 - 1;             begat[s]++;             begat[t + 1]--;         }         for (int i = 1; i <= len + 1; i++)begat[i] += begat[i - 1];     } };
  |