博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(回文串 )Best Reward -- hdu -- 3613
阅读量:6281 次
发布时间:2019-06-22

本文共 3656 字,大约阅读时间需要 12 分钟。

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: 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. 
 

 

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
#include
#include
#include
using namespace std;#define INF 0x3f3f3f3f#define N 1000007char s[N];int Left[N], Right[N], sum[N], p[N];void Manacher(){ int len = strlen(s); int index = 0, MaxLen = 0, i; for(i=2; i
i) p[i] = min(p[index*2-i], MaxLen-i); else p[i] = 1; 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]-1] = true; if(p[i]+i==len) Right[p[i]-1] = true; }}int main(){ int t; scanf("%d", &t); while(t--) { int a[30], i; memset(Left, 0, sizeof(Left)); memset(Right, 0, sizeof(Right)); for(i=0; i<26; i++) scanf("%d", &a[i]); scanf("%s", s); int len = strlen(s); for(i=1; i<=len; i++) sum[i] = sum[i-1] + a[s[i-1]-'a']; for(i=len; i>=0; i--) { s[i*2+2] = s[i]; s[i*2+1] = '#'; } s[0] = '$'; Manacher(); int ans = -INF; for(i=1; i
View Code

 

转载于:https://www.cnblogs.com/YY56/p/4853437.html

你可能感兴趣的文章
ART世界探险(19) - 优化编译器的编译流程
查看>>
玩转Edas应用部署
查看>>
music-音符与常用记号
查看>>
sql操作命令
查看>>
zip 数据压缩
查看>>
Python爬虫学习系列教程
查看>>
【数据库优化专题】MySQL视图优化(二)
查看>>
【转载】每个程序员都应该学习使用Python或Ruby
查看>>
PHP高级编程之守护进程,实现优雅重启
查看>>
PHP字符编码转换类3
查看>>
rsync同步服务配置手记
查看>>
http缓存知识
查看>>
Go 时间交并集小工具
查看>>
iOS 多线程总结
查看>>
webpack是如何实现前端模块化的
查看>>
TCP的三次握手四次挥手
查看>>
关于redis的几件小事(六)redis的持久化
查看>>
package.json
查看>>
webpack4+babel7+eslint+editorconfig+react-hot-loader 搭建react开发环境
查看>>
Maven 插件
查看>>