Best Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4345    Accepted Submission(s): 1791

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: 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
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
 
Source
 
Recommend
lcy

题意:

给定一个字符串,以及每个字母拥有的价值。现在要把这个字符串切成两半,如果子串是回文,那么就可以加上这个子串的字符价值之和,如果不是回文这个子串的价值就是0.

现在要求两个子串相加的最大价值。

思路:

用Manacher我们可以处理出,以某个节点$i$为中心时的回文串长度, 即$p[i]-1$。

如果暴力枚举分割点,如果子串的中心的$p[i] - 1$正好是子串的长度,那么说明这个子串是一个回文串。

这里由于我们要算每个字符的价值之和,可以预处理前缀和。

对于偶数长度的串和奇数长度的串分别求对应的价值。

 //#include<bits/stdc++>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h> #define LL long long
#define ull unsigned long long
#define inf 0x3f3f3f3f using namespace std; int t;
int val[];
const int maxlen = 5e5 + ;
char s[maxlen], s_new[maxlen * ];
int s_val[maxlen], p[maxlen * ]; int init()
{
int len = strlen(s + );
s_new[] = '$';
s_new[] = '#';
int j = ;
for(int i = ; i <= len; i++){
s_new[j++] = s[i];
s_new[j++] = '#';
}
s_new[j] = '\0';
return j;
} void manacher()
{
int len = init();
int id, mx = ; for(int i = ; i < len; i++){
if(i < mx)p[i] = min(p[ * id - i], mx - i);
else p[i] = ;
while(s_new[i - p[i]] == s_new[i + p[i]])p[i]++;
if(mx < i + p[i]){
id = i;
mx = i + p[i];
}
}
} int main()
{
scanf("%d", &t);
while(t--){
memset(p, , sizeof(p));
for(int i = ; i < ; i++){
scanf("%d", &val[i]);
}
scanf("%s", s + ); int len = strlen(s + );
for(int i = ; i <= len; i++){
s_val[i] = s_val[i - ] + val[s[i] - 'a'];
}
manacher();
// cout<<s_new<<endl;
// for(int i = 0; i <= len * 2; i++){
// cout<<p[i]<<" ";
// }
// cout<<endl; int ans = ;
for(int i = ; i < len; i++){
int tmp = ;
if(p[i + ] - == i){
if(i % ){
tmp += s_val[i / ] * + val[s[(i + ) / ] - 'a'];
}
else{
tmp += s_val[i / ] * ;
}
}
if(p[i + len + ] - == len - i){
if((len - i) % ){
tmp += (s_val[i + (len - i) / ] - s_val[i]) * + val[s[i + (len + - i) / ] - 'a'];
}
else{
tmp += (s_val[i + (len - i) / ] - s_val[i]) * ;
}
}
ans = max(ans, tmp);
}
printf("%d\n", ans);
} }

最新文章

  1. [LeetCode] Combinations 组合项
  2. MVVM架构~knockoutjs系列之Mapping插件为对象添加ko属性
  3. JavaScript 随机链接
  4. jquery放大镜效果
  5. Redis介绍以及安装(Linux)
  6. MySQL 按日期分表
  7. B - 娜娜梦游仙境系列——跳远女王
  8. C语言文件函数
  9. TQ210裸机编程(3)——按键(查询法)
  10. Java获取方法参数名、Spring SpEL解析
  11. WINDOWS Server2003上部署一个Asp.Net的网站
  12. AJAx 刷新页面
  13. 回锅的美食:JSP+EL+JSTL大杂烩汤
  14. IE6浏览器有哪些常见的bug,缺陷或者与标准不一致的地方,如何解决
  15. Android : Resource is not a Drawable (color or path)
  16. 高级javaScript程序形成过程(进阶)
  17. linux添加计划任务
  18. exe4j 使用记录(二):jar打包exe
  19. GOF23设计模式之访问者模式(visitor)
  20. 2014.9.30 Double转字符

热门文章

  1. 【Zookeeper】源码分析之网络通信(三)之NettyServerCnxn
  2. 使用Buildozer部署Kivy到移动设备上
  3. 〖Android〗arm-linux-androideabi-gdb报 libpython2.6.so.1.0: cannot open shared object file错误的解决方法
  4. JVM的7种垃圾收集器:主要特点 应用场景 设置参数 基本运行原理
  5. Android---Hellow World
  6. [k8s]coredns/kube-dns配置subdomain
  7. android sdk manager 代理设置
  8. 物联网架构成长之路(9)-双机热备Keepalived了解
  9. 3. ELMo算法原理解析
  10. 【WPF】图片按钮的单击与双击事件