标签:
Description
Input
Output
Sample Input
Sample Output
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int maxn = 500100; char s[maxn]; char txt[maxn<<1]; int p[maxn<<1]; int sum[maxn<<1]; int len; int val = 0; int n; int v[26]; int oo = 0x3f3f3f3f; void make() { txt[0] = ‘$‘; int pos = 0; for(int i = 0; i < len; i++) { txt[++pos] = ‘#‘; txt[++pos] = s[i]; }txt[++pos] = ‘#‘; n = len * 2 + 2; txt[n] = ‘\0‘; } int get(int id) { if(txt[id] >= ‘a‘ && txt[id] <= ‘z‘) { return v[txt[id] - ‘a‘]; } return 0; } void solve() { int maxx = 0, id; for(int i = 1; i < n; i++) { if(i < maxx) p[i] = min(p[id * 2 - i], p[id] + id - i); else p[i] = 1; while(txt[i - p[i]] == txt[i + p[i]]) { if(i + p[i] > maxx) { maxx = i + p[i]; id = i; } p[i]++; } } for(int i = 1; i < n; i++) sum[i] = sum[i - 1] + get(i); for(int i = 1; i < n; i++) p[i]--; int ans = -oo, tsum = 0; for(int i = 2; i < n - 1; i++) { tsum = 0; int l, r; int newl, newr; if(p[i]) { l = i - p[i]; r = i + p[i]; } else { l = i - 1, r = i + 1; } if(l != 1 && r != len * 2 + 1) { tsum = 0; l = 1; newl = r, newr = len * 2 + 1; } else if(l != 1 && r == len * 2 + 1) { newl = 1, newr = l; tsum = sum[r] - sum[l]; } else if(l == 1 && r != len * 2 + 1) { newl = r, newr = len * 2 + 1; tsum = sum[r] - sum[l]; } else { tsum = -oo; } int newmid = (newl + newr) / 2; //printf("check %d %d %d %d %d %d %d\n", l, r, newl, newr, tsum, ans, newmid); if(newmid - p[newmid] != 1 && newmid + p[newmid] != len * 2 + 1) { ans = max(ans, tsum); continue; } tsum += (sum[newr] - sum[newl]); ans = max(ans, tsum); } printf("%d\n", ans); } int main() { int t; scanf("%d", &t); while(t--) { for(int i = 0; i < 26; i++) scanf("%d", &v[i]); scanf("%s", s); memset(p, 0, sizeof(p)); memset(sum, 0, sizeof(sum)); len = strlen(s); make(); solve(); } }
hdu-3613 Best Reward (manacher算法)
标签:
原文地址:http://www.cnblogs.com/lonewanderer/p/5651556.html