Best Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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

题目大意:

     有一个由26种宝石组成的项链,给定每一种宝石的的价值,现需要将这串项链分成两部分

     如果分开的项链是一个回文的则它的价值为之一部分项链上所有宝石价值的总和,否则的话它的价值为0

     求能够得到的最大价值是多少。

解题思路:

     先算出这串项链的价值前缀和,然后用manachar算法求出项链的p数组,根据p数组枚举切割点,从而求得最大价值。

     manachar算法的具体讲解参考:http://www.cnblogs.com/yoke/p/6938193.html

AC代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; const int N = ;
char s[N*]; // 项链
int val[], p[N*], sum[N]; // val存每种宝石的价值 sum存项链的价值前缀和(前i个字符价值和)
int pre[N], pos[N]; // pre标记前i个字符为回文串 pos标记后i个字符为回文串
int manachar ()
{
memset(pos,,sizeof(pos));
memset(pre,,sizeof(pre));
memset(sum,,sizeof(sum));
int i, j = ;
int len = strlen(s);
for (i = ; i <= len; i ++) // 求项链价值的前缀和
sum[i] += sum[i-]+val[s[i-]-'a']; for (i = len; i >= ; i --) // 预处理项链字符串
{
s[i*+] = s[i];
s[i*+] = '#';
}s[] = '@';
// puts(s); int id = , mx = ;
for (i = ; s[i]; i ++)
{
p[i] = i<mx? min(p[id*-i],mx-i) : ;
while (s[i+p[i]] == s[i-p[i]]) p[i] ++;
if (i+p[i] > mx){
id = i;
mx = i + p[i];
}
if (i == p[i]) pre[p[i]-] = ; //表示前缀(前p[i]-1个字符)是回文串
if (i+p[i] == *len+) pos[p[i]-] = ; //表示后缀(后p[i]-1个字符)是回文串
}
int ans = -0x3f3f3f3f;
for (i = ; i < len; i ++) // 不断枚举切割点 求最大价值
{
j = ;
if (pre[i]) j += sum[i];
if (pos[len-i]) j += sum[len]-sum[i];
if (j > ans) ans = j;
}
return ans;
}
int main ()
{
int t,i;
scanf("%d",&t);
while (t --)
{
for (i = ; i < ; i ++)
scanf("%d",&val[i]);
scanf("%s",s);
int ans = manachar();
printf("%d\n",ans);
}
return ;
}

hdu 3613 Best Reward (manachar算法)的更多相关文章

  1. hdu 3613"Best Reward"(Manacher算法)

    传送门 题意: 国王为了犒劳立下战功的大将军Li,决定奖给Li一串项链,这个项链一共包含26中珠子"a~z",每种珠子都有 相应的价值(-100~100),当某个项链可以构成回文时 ...

  2. HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

  3. HDU 3613 Best Reward 正反两次扩展KMP

    题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...

  4. HDU - 3613 Best Reward(manacher或拓展kmp)

    传送门:HDU - 3613 题意:给出26个字母的价值,然后给你一个字符串,把它分成两个字符串,字符串是回文串才算价值,求价值最大是多少. 题解:这个题可以用马拉车,也可以用拓展kmp. ①Mana ...

  5. HDU 3613 Best Reward(拓展KMP算法求解)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

  6. 扩展KMP --- HDU 3613 Best Reward

    Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...

  7. HDU 3613 Best Reward(manacher求前、后缀回文串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...

  8. HDU 3613 Best Reward(扩展KMP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3613 [题目大意] 一个字符串的价值定义为,当它是一个回文串的时候,价值为每个字符的价值的和,如果 ...

  9. HDU 3613 Best Reward(扩展KMP求前后缀回文串)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...

随机推荐

  1. 【笔记】Django的视图

    [笔记]Django的视图 Python Django  Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应 ...

  2. 【测试的艺术】+测试分析&测试计划+模板

    一.项目概述 1.1.项目背景 #就是说一下为什么要做这个项目 1.2.项目目标 #这个项目最终要达到的目标是什么 二.项目整体分析 #项目分为哪些部分?各部分之间的关联是什么?各部分的目标是什么? ...

  3. Rancher 1.6 版本 只能在 linux 下用

    实际操作 启动 , 访问方式 : 在启动过程中会发现没有 image , 然后自动下载 ( 执行 docker pull 命令 ) docker run --rm --privileged -v /v ...

  4. 2016424王启元 Exp2 后门原理与实践

    一.实验准备 1.在实验前关闭或退出了防火墙.360杀毒软件.电脑卫士等所有的电脑保护软件,避免在实验过程中攻击时被拒绝.       2.使用Windows获linux shell (1)在Wind ...

  5. selenium+Python(处理html5的视频播放)

    Webdriver支持在指定的浏览器测试HTML5,另外可以用JavaScript来测试这些功能,这样就可以在任何浏览器上测试HTML5 多数浏览器使用控件来播放视频,但是不同浏览器需要使用不同的插件 ...

  6. how to be an efficient man

    "This Monday I was invited to do a presentation on this Friday, and today is Friday. I am going ...

  7. Ajax(Asychronous JavaScript and XML)笔记

    1 Ajax简介 1 ajax概念 2 什么是同步?什么是异步? 3 ajax原理 2 JavaScript原生的ajax 1 ajax.html代码 <!DOCTYPE html> &l ...

  8. plsql数据库异常---plsql 登录后,提示数据库字符集(AL32UTF8)和客户端字符集(ZHS16GBK)不一致

    今天遇到这个问题网上搜了一下答案找到了 转贴 http://blog.csdn.net/lidew521/article/details/8546155 plsql 登录后提示: Database c ...

  9. Python对日期进行格式化

    Python对日期进行格式化 把当前时间输出为2017-04-07 19:00:00.进入python交互命令行输入: > import datetime > currtime = dat ...

  10. 九度oj 1002 Grading 2011年浙江大学计算机及软件工程研究生机试真题

    #include<iostream> #include<queue> #include<cstdio> #include<cstring> #inclu ...