In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you that a string is called a palindrome if it can be read the same way both from left to right and from right to left. Here are examples of such strings: «eye», «pop», «level», «aba», «deed», «racecar», «rotor», «madam».
Nick started to look carefully for all palindromes in the text that they were reading in the class. For each occurrence of each palindrome in the text he wrote a pair — the position of the beginning and the position of the ending of this occurrence in the text. Nick called each occurrence of each palindrome he found in the text subpalindrome. When he found all the subpalindromes, he decided to find out how many different pairs among these subpalindromes cross. Two subpalindromes cross if they cover common positions in the text. No palindrome can cross itself.
#include<cstdio> #include<cstring> #include<algorithm> constint mod = 51123987; constint MAXN = 2000001; int m; char w[2000010]; int d[MAXN][2]; int start[MAXN], finish[MAXN], qa[MAXN], qb[MAXN]; longlong sstart[MAXN]; inlineintget(int i){ return d[(i + 1) >> 1][i & 1] == ((i + 1) >> 1); } inlinevoidsolve(constint n){ gets(w); longlong all = 0; for (registerint t = 0; t < 2; t++) { registerint l = 0, r = -1, j; for (registerint i = 0; i < n; i++) { if (i > r) j = 1; else j = std::min (d[l + r - i + t][t], r - i + t) + 1; while (i + j - t < n && i - j >= 0 && w[i + j - t] == w[i - j]) j++; d[i][t] = --j; if (i + j + t > r) { l = i - j; r = i + j - t; } registerint x = i - d[i][t], y = i - t + d[i][t]; if (x <= y) { all += d[i][t] + (1 - t), qa[x]++; if (i - t + 1 < n) qa[i - t + 1]--; if (i > 0) qb[i - 1]--; qb[y]++; } } } registerlonglong all2 = all - 1; if (~all & 1) all >>= 1; else all2 >>= 1; all = (all % mod) * (all2 % mod), all %= mod; registerint cur = 0; for (registerint i = 0; i < n; i++) cur += qa[i], start[i] = cur; cur = 0; for (registerint i = n - 1; i >= 0; i--) cur += qb[i], finish[i] = cur; registerlonglong sum = 0; for (registerint i = 0; i + 1 < n; i++) sum += finish[i], all = (all - (sum * start [i + 1]) % mod + mod) % mod; printf("%d\n", (int)(all % mod)); } intmain(){ int n; scanf ("%d\n", &n); solve(n); return0; }