【题解】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 |
样例输出
1 | 1 |
提示
无
思路
将一个字符串分为两部分,若某一子串是回文串,则该子串的价值为各字符的权值和,否则价值为0。求一种分割方法,使得两部分价值和最大。
首先跑一遍Manacher,对于原串的前缀回文子串,满足中心点i==p[i]
,而对于原串的后缀回文子串,满足中心点i+p[i] == 处理过的原串长度 即2*len(原串)+2
。i为处理过的字符串数组下标,p数组是最大回文半径。
所以枚举n+n-1个回文中心,看是否是原串的前缀回文子串,讨论剩下的部分,若剩下部分是回文串,则可能的回文中心r = (处理过的原串长度n + 前缀回文子串的右端点i+p[i]-1) / 2
,以此判断后半部分是否为回文串,取最大价值和即可。获取回文子串的价值可以用前缀和优化。
代码
1 | char s[mxn], t[mxn]; |