CodeChef居然提供了中文题面
建出PAM后,在Fail数上DFS,走到一个点时看它的祖先中有没有长度为 ⌊len2⌋ ⌊ l e n 2 ⌋ 的,记录权值,乘上 size s i z e 即可。
/************************************************
* Au: Hany01
* Date: Jun 22nd, 2018
* Prob: CodeChef PALPROB
* Email: [email protected]
* Institute: Yali High School
************************************************/
#include
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 100005;
int beg[maxn], nex[maxn], e, v[maxn], len[maxn], pos[maxn], val[maxn], sz[maxn];
LL Ans;
inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }
void dfs(int u)
{
if (u > 1) pos[len[u]] = u, Ans += (LL)sz[u] * (val[u] = val[pos[len[u] / 2]] + 1);
for (register int i = beg[u]; i; i = nex[i]) dfs(v[i]);
if (u > 1) pos[len[u]] = 0;
}
int main()
{
#ifdef hany01
File("PALPROB");
#endif
static int T, n, fa[maxn], ch[maxn][26], tot, las;
static char s[maxn];
fa[1] = fa[0] = 1, len[1] = -1;
for (T = read(); T --; )
{
scanf("%s", s + 1), n = strlen(s + 1), Set(ch, 0), Set(beg, 0), e = 0, Set(val, 0), Set(sz, 0), tot = 1;
For(i, 1, n) {
register int p = las, c = s[i] - 97;
while (s[i] != s[i - len[p] - 1]) p = fa[p];
if (!ch[p][c]) {
register int np = ++ tot, k = fa[p];
while (s[i] != s[i - len[k] - 1]) k = fa[k];
len[np] = len[p] + 2, fa[np] = ch[k][c], ch[p][c] = np;
}
++ sz[las = ch[p][c]];
}
add(1, 0);
Fordown(i, tot, 2) sz[fa[i]] += sz[i], add(fa[i], i);
Ans = 0, dfs(1);
printf("%lld\n", Ans);
}
return 0;
}
//四月南风大麦黄,枣花未落桐叶长。
// -- 李颀《送陈章甫》