【题解】HDU-3613 Best Reward

Best Reward(HDU-3613)

题面

After an uphill battle, General Li won a great victory. Now the head of state decide to reward him with honor and treasures for his great exploit.

One of these treasures is a necklace made up of 26 different kinds of gemstones, and the length of the necklace is n. (That is to say: n gemstones are stringed together to constitute this necklace, and each of these gemstones belongs to only one of the 26 kinds.)

In accordance with the classical view, a necklace is valuable if and only if it is a palindrome - the necklace looks the same in either direction. However, the necklace we mentioned above may not a palindrome at the beginning. So the head of state decide to cut the necklace into two part, and then give both of them to General Li.

All gemstones of the same kind has the same value (may be positive or negative because of their quality - some kinds are beautiful while some others may looks just like normal stones). A necklace that is palindrom has value equal to the sum of its gemstones’ value. while a necklace that is not palindrom has value zero.

Now the problem is: how to cut the given necklace so that the sum of the two necklaces’s value is greatest. Output this value.

输入

The first line of input is a single integer T (1 ≤ T ≤ 10) - the number of test cases. The description of these test cases follows.

For each test case, the first line is 26 integers: v1, v2, …, v26 (-100 ≤ vi ≤ 100, 1 ≤ i ≤ 26), represent the value of gemstones of each kind.

The second line of each test case is a string made up of charactor ‘a’ to ‘z’. representing the necklace. Different charactor representing different kinds of gemstones, and the value of ‘a’ is v1, the value of ‘b’ is v2, …, and so on. The length of the string is no more than 500000.

输出

Output a single Integer: the maximum value General Li can get from the necklace.

样例输入

1
2
3
4
5
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
2
1
6

提示

思路

将一个字符串分为两部分,若某一子串是回文串,则该子串的价值为各字符的权值和,否则价值为0。求一种分割方法,使得两部分价值和最大。

首先跑一遍Manacher,对于原串的前缀回文子串,满足中心点i==p[i],而对于原串的后缀回文子串,满足中心点i+p[i] == 处理过的原串长度 即2*len(原串)+2。i为处理过的字符串数组下标,p数组是最大回文半径。

所以枚举n+n-1个回文中心,看是否是原串的前缀回文子串,讨论剩下的部分,若剩下部分是回文串,则可能的回文中心r = (处理过的原串长度n + 前缀回文子串的右端点i+p[i]-1) / 2,以此判断后半部分是否为回文串,取最大价值和即可。获取回文子串的价值可以用前缀和优化。

代码

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
char s[mxn], t[mxn];
int p[mxn];

int manacher_init(char *s, char *t, int n)
{
int j = 2; t[0] = '$', t[1] = '#';
for (int i=0; i<n; i++)
{
t[j++] = s[i];
t[j++] = '#';
}
t[j] = '\0';
return j;
}

int manacher(char *t, int *p, int n)
{
int id = 0, mx = 0, ans = 0;
for (int i=1; i<=n; i++)
{
p[i] = mx>i ? min(p[2*id-i], mx-i) : 1;

while (t[i+p[i]] == t[i-p[i]]) p[i]++;

if (mx < i+p[i])
mx = i+p[i], id = i;

ans = max(ans, p[i]);
}
return ans-1;
}

int a[30];
int sum[mxn];

int main()
{
int T; scanf("%d", &T);
while(T--)
{
for(int i=0; i<26; i++)
scanf("%d", &a[i]);
scanf("%s", s);
int n = manacher_init(s, t, strlen(s));
manacher(t, p, n);

/* 前缀和 */
sum[0] = sum[1] = 0;
for(int i=2; i<n; i++){
sum[i] = i&1 ? sum[i-1] : sum[i-2] + a[t[i]-'a'];
}

int ans = 0;
for(int i=2; i<n; i++) // 枚举前半部分回文中心
{
int a=0, b=0;
if(i==p[i]){ // 如果前半部分是回文串
a = sum[i+p[i]-1];
}

int r = (n + i+p[i]-1)/2; // 获得后半部分回文中心
if(r == n-1) // 前半部分为原串,没有后半部分
continue;

if(r+p[r] == n){ // 如果后半部分是回文串
b = sum[n-2] - sum[i+p[i]-1];
}
ans = max(ans, a+b);
}
printf("%d\n", ans);
}

return 0;
}
_/_/_/_/_/ EOF _/_/_/_/_/