http://acm.hdu.edu.cn/showproblem.php?pid=3613

Best Reward

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

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
 
给你一个字符串,每个字符有它们各自的价值,把这个串分成两部分(随意分割),如果这部分是回文串的话计算它的价值,否则价值为0, 计算它能得到的最大价值

/**
s[]         先存原字符串,后存扩展后的字符串
p[]        p[i] 表示以i为中心的回文串有多长(只记录一边的长度)
sum[]    sum[i]表示前i个字符的总价值和
Left[]    Left[i] 表示前缀长度为 i 的串是否是回文串
Right[]  Right[i] 表示后缀长度为 i 的串是否是回文串
**/

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm> using namespace std;
#define INF 0x3f3f3f3f
#define N 1000007 char s[N];
int Left[N], Right[N], sum[N], p[N]; void Manacher()
{
int len = strlen(s); int index = , MaxLen = , i; for(i=; i<len; i++)
{
if(MaxLen>i) p[i] = min(p[index*-i], MaxLen-i);
else p[i] = ; while( s[i-p[i]]==s[i+p[i]]) p[i]++; if(p[i]+i>MaxLen)
{
MaxLen = p[i] + i;
index = i;
} if(p[i]==i)
Left[p[i]-] = true;
if(p[i]+i==len)
Right[p[i]-] = true;
}
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int a[], i; memset(Left, , sizeof(Left));
memset(Right, , sizeof(Right)); for(i=; i<; i++)
scanf("%d", &a[i]); scanf("%s", s); int len = strlen(s); for(i=; i<=len; i++)
sum[i] = sum[i-] + a[s[i-]-'a']; for(i=len; i>=; i--)
{
s[i*+] = s[i];
s[i*+] = '#';
}
s[] = '$'; Manacher(); int ans = -INF;
for(i=; i<len; i++)
{
int t = ;
if(Left[i])
t += sum[i];
if(Right[len-i])
t += sum[len]-sum[i]; ans = max(ans, t);
} printf("%d\n", ans); }
return ;
}

最新文章

  1. featherview模板引擎
  2. Linux学习 :字符设备框架
  3. java8中map的meger方法的使用
  4. Tomcat编码问题及访问软链接文件设置
  5. NOI 题库 2753
  6. CentOS6.5菜鸟之旅:安装ATI显卡驱动
  7. ORA-01207: file is more recent than control file -
  8. Android应用程序中应用图标和名字的设置
  9. 学习笔记1_Day09_Servlet
  10. apache 反向代理配置(ubuntu)
  11. SqlServer2008(R2) 数据库使用外网IP实例连接服务器
  12. Azure 网站的新增功能:可配置的环境变量
  13. 基于visual Studio2013解决C语言竞赛题之1012连接字符串
  14. hightchart导出图片
  15. Ztree改版 - 将图标字体化 - fontAwesome
  16. SpringCloud学习之SpringCloudBus
  17. [HTML/CSS]浮动的那点事儿
  18. ajax---获取XMLHttpReuquest 对象
  19. Java == 和 equals 区别
  20. 一些简单二分题,简单的hash,H(i),字符串题

热门文章

  1. JSP复习(part 3 )
  2. C#累加器函数Aggregate用法 讲解
  3. 实现Action的三种方式
  4. Concurrency and Race Conditions
  5. unity profiler - Loading.ReadObject
  6. python笔记之循环控制
  7. 利用redis完成自动补全搜索功能(一)
  8. VideoView的全屏问题
  9. discuz回贴通知插件实现-显示用户状态设置
  10. 分享chrome清空缓存开发小技巧