2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 aba 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 acacac
1 6
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int const MAX = 500005; char s[MAX << 1]; int p[MAX << 1], val[30], sum[MAX], len; bool suf[MAX], pre[MAX]; void Manacher() { int maxp = 0, maxl = 0; for(int i = len; i >= 0; i--) { s[i * 2 + 2] = s[i]; s[i * 2 + 1] = '#'; } s[0] = '*'; for(int i = 2; i < 2 * len + 1; i++) { if(p[maxp] + maxp > i) p[i] = min(p[2 * maxp - i], p[maxp] + maxp - i); else p[i] = 1; while(s[i - p[i]] == s[i + p[i]]) p[i]++; if(p[maxp] + maxp < i + p[i]) maxp = i; if(i - p[i] == 0) pre[p[i] - 1] = true; if(i + p[i] == 2 * len + 2) suf[p[i] - 1] = true; } } int main() { int T; scanf("%d", &T); while(T--) { int ans = -0xfffffff; memset(suf, false, sizeof(suf)); memset(pre, false, sizeof(pre)); memset(sum, 0, sizeof(sum)); for(int i = 0; i < 26; i++) scanf("%d", &val[i]); scanf("%s", s); len = strlen(s); for(int i = 1; i <= len; i++) sum[i] = sum[i - 1] + val[s[i - 1] - 'a']; Manacher(); for(int i = 1; i < len; i++) { int tmp = 0; if(pre[i]) tmp += sum[i]; if(suf[len - i]) tmp += sum[len] - sum[i]; ans = tmp > ans ? tmp : ans; } printf("%d\n", ans); } }
HDU 3616 Best Reward (Manacher算法 前缀回文+后缀回文)
原文地址:http://blog.csdn.net/tc_to_top/article/details/43811693