Best Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1361 Accepted Submission(s): 551
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.
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.
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 6
题意是给定一个只含26个字母的权值,然后给定一个字符串,问如何将该字符串分成两块,且让这两块的字符串的权值和最大。一个字符串权值大小的规则是判断其是否是一个回文串,如果是权值就是其长度,如果不是,权值为0。
回文串可以考虑使用Manacher算法,对整个串进行回文串的判定,然后遍历,对于每个二分点,判断其两边是否是回文串并标记。最后再次遍历找到最大值。
代码如下:
/************************************************************************* > File Name: Best_Reward.cpp > Author: Zhanghaoran > Mail: chilumanxi@xiyoulinux.org > Created Time: 2016年02月24日 星期三 21时42分37秒 ************************************************************************/ #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; const int MAXN = 500010; int inde[30]; char str[MAXN]; char Ma[MAXN * 2]; int Mp[MAXN * 2]; int len; long long value[MAXN]; bool flaga[MAXN]; bool flagb[MAXN]; void Manacher(){ int l = 0; Ma[l ++] = '$'; Ma[l ++] = '#'; for(int i = 0; i < len; i ++){ Ma[l ++] = str[i]; Ma[l ++] = '#'; } Ma[l] = 0; int mx = 0, id = 0; for(int i = 0; i < l; i ++){ Mp[i] = mx > i ? min(Mp[2 * id - i], mx - i) : 1; while(Ma[i + Mp[i]] == Ma[i - Mp[i]]) Mp[i] ++; if(i + Mp[i] > mx){ mx = i + Mp[i]; id = i; } if(i - Mp[i] == 0) flaga[Mp[i] - 1] = true; if(i + Mp[i] == len + len + 2) flagb[Mp[i] - 1] = true; } } int T; int main(void){ cin >> T; while(T --){ for(int i = 0; i < 26; i ++){ cin >> inde[i]; } memset(flaga, false, sizeof(flaga)); memset(flagb, false, sizeof(flagb)); cin >> str; len = strlen(str); for(int i = 1; i <= len; i ++){ value[i] = value[i - 1] + inde[str[i - 1] - 'a']; } Manacher(); long long ans = 0; long long temp = 0; for(int i = 1; i < len; i ++){ if(flaga[i] == true) temp += value[i]; if(flagb[len - i] == true) temp += value[len] - value[i]; ans = ans > temp ? ans : temp; temp = 0; } cout << ans << endl; } }