题目:
给一个字符串。求满足s[a…b], s[c…d]为回文串的元组(a, b, c, d) (
a<=b<c<=d
)
的数目
思路:
从求最长回文串的DP方法拓展而来。
用ok[i][j]记录s[i…j]是否为回文串,sum[i]为回文串的前缀和。
dp[i] = dp[i-1] + ok[j][i] * pre[j-1],
1<=j<=i
LL solve() {
int len = s.length();
rep(i, 0, len-1) ok[i][i] = 1;
rep(i, 0, len-2) ok[i][i+1] = s[i] == s[i+1];
rep(k, 3, len) rep(i, 0, len-k)
ok[i][i+k-1] = ok[i+1][i+k-2] && (s[i]==s[i+k-1]);
pre[0] = 1;
rep(i, 1, len-1) {
pre[i] = 0;
rep(j, 0, i) if (ok[j][i]) pre[i] += 1;
pre[i] += pre[i-1];
}
dp[0] = 0;
rep(i, 1, len-1) {
dp[i] = dp[i-1];
rep(j, 1, i)
dp[i] += ok[j][i] * pre[j-1];
}
return dp[len-1];
}