#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") // alphabet is [OFFSET, OFFSET + SIZE), with sentinel (OFFSET - 1) template struct Pam { struct Node { // ts[pos, pos + len]: suffix when created // fail: longest proper palindromic suffix int len, pos, fail; int nxt[SIZE]; }; // nodes[0]: length 0 (also means null in nxt) // nodes[1]: length -1 // nodes[2, nodesLen): non-empty palindromic substring // suf: node for longest palindromic suffix int nodesLen, suf; vector nodes; // current whole string: ts[0, r) (0 <= r <= nR) int nR, r; vector tsBuffer; T *ts; Pam(int nR_) : nR(nR_) { nodesLen = 2; suf = 0; nodes.resize(2 + nR); nodes[0].len = 0; nodes[0].pos = 0; nodes[0].fail = 1; nodes[1].len = -1; nodes[1].pos = 0; nodes[1].fail = 1; memset(nodes[0].nxt, 0, sizeof(nodes[0].nxt)); memset(nodes[1].nxt, 0, sizeof(nodes[1].nxt)); r = 0; tsBuffer.assign(1 + nR, OFFSET - 1); ts = tsBuffer.data() + 1; } const Node &operator[](int u) const { return nodes[u]; } void pushBack(T t) { assert(r < nR); const int a = t - OFFSET; ts[r++] = t; for (; ts[r - 2 - nodes[suf].len] != t; suf = nodes[suf].fail) {} Node &f = nodes[suf]; if (!f.nxt[a]) { Node &g = nodes[nodesLen]; g.len = f.len + 2; g.pos = r - g.len; int u = f.fail; for (; ts[r - 2 - nodes[u].len] != t; u = nodes[u].fail) {} g.fail = nodes[u].nxt[a]; memset(g.nxt, 0, sizeof(g.nxt)); f.nxt[a] = nodesLen++; // this needs to be after setting g.fail } suf = f.nxt[a]; } void dfsPrint(ostream &os, int u, const string &branch, int type) const { const Node &f = nodes[u]; os << branch << ((type == 0) ? "" : (type == 1) ? "|-- " : "`-- "); if (f.len <= 0) { os << "(" << f.len << ")"; } else { for (int i = f.pos; i < f.pos + f.len; ++i) os << ts[i]; } os << " " << u << " " << f.fail; // debug here os << "\n"; int a0 = -1; for (int a = 0; a < SIZE; ++a) if (f.nxt[a]) a0 = a; for (int a = 0; a < SIZE; ++a) if (f.nxt[a]) { dfsPrint(os, f.nxt[a], branch + ((type == 0) ? "" : (type == 1) ? "| " : " "), (a == a0) ? 2 : 1); } } friend ostream &operator<<(ostream &os, const Pam &pam) { pam.dfsPrint(os, 0, " ", 0); pam.dfsPrint(os, 1, "", 0); return os; } }; int N; char S[200'010]; int main() { for (; ~scanf("%s", S); ) { N = strlen(S); Pam pam(N); vector us(N + 1); us[0] = 0; for (int i = 0; i < N; ++i) { pam.pushBack(S[i]); us[i + 1] = pam.suf; } // cerr< dp(pam.nodesLen, 0); for (int i = 1; i <= N; ++i) ++dp[us[i]]; // cerr<<"dp = "<= 2; ) dp[pam.nodes[u].fail] += dp[u]; // cerr<<"dp = "<