标签:
Description:
Input:
Output:
Sample Input:
Sample Output:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int N=1e6+10; const int INF=0x3f3f3f3f; char S[N], s[N]; int r[N], a[30], sum[N], L[N], R[N], k; ///R数组标记接首位的回文串(下标是回文串长度),L数组标记接尾位的回文串 void Manacher() { int i, mx, id = 0; for (i = 2; i < k; i++) { r[i] = 1; mx = r[id]+id; if (mx > i) r[i] = min(r[2*id-i], mx-i); while (s[i-r[i]] == s[i+r[i]]) r[i]++; if (mx < r[i]+i) id = i; if (r[i] == i) L[r[i]-1] = 1; ///因为r保存的是半径,如果半径等于i,那么肯定接首位 if (r[i]+i == k) R[r[i]-1] = 1; ///r[i]+i是以i为中心的回文串的边界,如果等于k,则接尾位 } } int main() { int T, i, ls, ans, num; scanf("%d", &T); while (T--) { k = 2; memset(r, 0, sizeof(r)); memset(sum, 0, sizeof(sum)); memset(L, 0, sizeof(L)); memset(R, 0, sizeof(R)); for (i = 0; i < 26; i++) scanf("%d", &a[i]); scanf("%s", S); ls = strlen(S); for (i = 1; i <= ls; i++) sum[i] = sum[i-1] + a[S[i-1]-‘a‘]; ///先将价值之和保存下来 s[0] = ‘$‘; s[1] = ‘#‘; for (i = 0; S[i] != ‘\0‘; i++) { s[k++] = S[i]; s[k++] = ‘#‘; } Manacher(); ans = a[S[0]-‘a‘]; for (i = 1; i < ls; i++) { num = 0; if (L[i]) num = sum[i]; if (R[ls-i]) num += (sum[ls]-sum[i]); ans = max(ans, num); } printf("%d\n", ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/syhandll/p/4871177.html