Best Reward
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other)
Problem Description 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.
Input 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: v[sub]1[/sub], v[sub]2[/sub], ..., v[sub]26[/sub] (-100 ≤ v[sub]i[/sub] ≤ 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[sub]1[/sub], the value of 'b' is v[sub]2[/sub], ..., and so on. The length of the string is no more than 500000.
Output Output 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
acacac
Sample Output
1
6
//题意:一根项链(直的,不是环状)上有26个宝石,(总共有26种宝石,每种宝石有它的价值),现在要求把项链分成2部分(一刀两断,即宝石顺序不能换),如果是回文的,价值等于上面所有宝石价值的总和,如果不回文,价值等于0。(回文:顺序和逆序是一样的,如aba,abcba等等)求被分开后2段项链的最大总价值。
//思路:很暴力的把给的字符串按字符一个一个枚举分过去(如给的字符串是aba,暗a ba、ab a这样枚举),现在的问题是怎么高效的去判断是不是回文的,最朴素的把头和尾去比肯定TLE,这里用拓展KMP去判断是否回文。
//拓展KMP介绍:http://blog.csdn.net/dyx404514/article/details/41831947
怎么用拓展KMP去判断回文:
把字符串str[]逆序得rstr[],用str[]去匹配rstr[],得到extend1[],若extend1[i]+i==len,说明前缀是回文的; 用rstr[]去匹配str[],得到extend2[],若extend2[i]+i==len,说明后缀是回文的。
(abcba其实是abcb a、abc ba、ab cba、a bcba这样分的)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 500000+100;
int val[30];
char str[MAX];
char rstr[MAX];
int Next1[MAX], Next2[MAX];
int extend1[MAX], extend2[MAX];
void GetNext(char *s, int *Next)
{
int i = 0, j, po, len = strlen(s);
Next[0] = len;
while (s[i] == s[i + 1] && i + 1 < len)
i++;
Next[1] = i;
po = 1;
for (i = 2; i < len; i++)
{
if (Next[i - po] + i < Next[po] + po)
Next[i] = Next[i - po];
else
{
j = Next[po] + po - i;
if (j < 0)
j = 0;
while (i + j < len&&s[j] == s[i + j])
j++;
Next[i] = j;
po = i;
}
}
}
void ExKmp(char *s1, char *s2, int *Next, int *extend)
{
int i = 0, j, po, len1 = strlen(s1), len2 = strlen(s2);
GetNext(s2,Next);
while (s1[i] == s2[i] && i < len2&&i < len1)
i++;
extend[0] = i;
po = 0;
for (i = 1; i < len1; i++)
{
if (Next[i - po] + i < extend[po] + po)
extend[i] = Next[i - po];
else
{
j = extend[po] + po - i;
if (j < 0)
j = 0;
while (i + j < len1&&j < len2&&s1[i + j] == s2[j])
j++;
extend[i] = j;
po = i;
}
}
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int ans = -200;
int sum[MAX];
memset(Next1, 0, sizeof(Next1));
memset(Next2, 0, sizeof(Next2));
memset(extend1, 0, sizeof(extend1));
memset(extend2, 0, sizeof(extend2));
for (int i = 0; i < 26; i++)
scanf("%d", &val[i]);
scanf("%s", str);
int len = strlen(str);
sum[0] = val[str[0] - 'a'];
for (int i = 1; i < len; i++)
sum[i] = sum[i - 1] + val[str[i] - 'a'];
for (int i = 0; i < len; i++)
rstr[i] = str[len - i - 1];
rstr[len] = '\0';
ExKmp(rstr, str, Next1, extend1);//前缀
ExKmp(str, rstr, Next2, extend2);//后缀
for (int i = 1; i < len; i++)
{
int tot = 0;
int j = len - i;
if (i + extend1[i] == len)
tot += sum[len - 1 - i];
if (j + extend2[j] == len)
tot += (sum[len - 1] - sum[j - 1]);
if (tot > ans)
ans = tot;
}
printf("%d\n", ans);
}
return 0;
}