HDU 3613 Best Reward(拓展KMP算法求解)
Posted wenzhixin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 3613 Best Reward(拓展KMP算法求解)相关的知识,希望对你有一定的参考价值。
题目链接:
https://cn.vjudge.net/problem/HDU-3613
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.
InputThe 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: v 1, v 2, ..., v 26 (-100 ≤ v i ≤ 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 v 1, the value of ‘b‘ is v 2, ..., and so on. The length of the string is no more than 500000.
OutputOutput a single Integer: the maximum value General Li can get from the necklace.Sample Input
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 acacacSample Output
1 6
解题思路:
问题的关键还是如何判断从某个位置分割开后,前缀和后缀是否是回文串
这次采用拓展kmp算法求解,关于拓展kmp的介绍请参考另一篇博客:https://www.cnblogs.com/wenzhixin/p/9355480.html
先将s1反转赋值给s2,用s1去匹配s2,得到s2中每个后缀与s1的最长公共前缀数组extpre
用s2去匹配s1,得到s1中每个后缀与s2的最长公共前缀数组extpos
再枚举每一个分割点,判断,求值,更新答案取最优。
关键是如何使用extend数组,举个例子来说
abcde
当分割长度为2的时候,需要分别判断ab和bcde是否是回文串
先看ab,要看ab是否是回文串需要用到extpre数组,为什么用到它而不是另一个分析如下:
用s2去匹配s1制作的到extpre数组
s2 edcba
s1 abcde
要判断ab是否是回文串,就是看ba与s1的最长公共前缀是多少,如果恰好是ba的长度,就说明ba在原串中存在。也即extpre[ls - i] == i(其中ls是串的总长度)。同时又有两者逆序,必然是回文串。
再看bcde是否是回文串
用s1去匹配s2制作的到extpos数组
s1 abcde
s2 edcba
要判断bcde是否是回文串,就要看bcde与s2的最长公共前缀是多少,如果恰好是dcde的长度,就说明bcde在反串中存在。也即extpre[i] == ls - i(其中ls是串的总长度)。同时又有两者逆序,必然是回文串。
这样就完成了,是否是回文串的判断。
代码实现:
1 #include<cstdio> 2 #include<cstring> 3 4 const int maxn = 500010; 5 int val[26], presum[maxn], next[maxn], extpre[maxn], extpos[maxn]; 6 char s1[maxn], s2[maxn]; 7 8 void exkmp(char s[], char t[], int len, int ex[]); 9 void get_next(char t[], int len); 10 11 int main() 12 { 13 int T; 14 scanf("%d", &T); 15 16 while(T--){ 17 for(int i = 0; i < 26; i++){ 18 scanf("%d", &val[i]); 19 } 20 scanf("%s", s1); 21 int ls = strlen(s1); 22 presum[0] = 0; 23 for(int i = 0;i < ls; i++){ 24 s2[ls - 1 - i] = s1[i]; 25 presum[i + 1] = presum[i] + val[s1[i] - ‘a‘]; 26 } 27 s2[ls] = ‘