hdu 3613 Best Reward (manachar算法)
Best Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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.
题目大意:
有一个由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算法)的更多相关文章
- hdu 3613"Best Reward"(Manacher算法)
传送门 题意: 国王为了犒劳立下战功的大将军Li,决定奖给Li一串项链,这个项链一共包含26中珠子"a~z",每种珠子都有 相应的价值(-100~100),当某个项链可以构成回文时 ...
- HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
- HDU 3613 Best Reward 正反两次扩展KMP
题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...
- HDU - 3613 Best Reward(manacher或拓展kmp)
传送门:HDU - 3613 题意:给出26个字母的价值,然后给你一个字符串,把它分成两个字符串,字符串是回文串才算价值,求价值最大是多少. 题解:这个题可以用马拉车,也可以用拓展kmp. ①Mana ...
- HDU 3613 Best Reward(拓展KMP算法求解)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- HDU 3613 Best Reward(manacher求前、后缀回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...
- HDU 3613 Best Reward(扩展KMP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3613 [题目大意] 一个字符串的价值定义为,当它是一个回文串的时候,价值为每个字符的价值的和,如果 ...
- HDU 3613 Best Reward(扩展KMP求前后缀回文串)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...
随机推荐
- c++开发规范
目录 1. 头文件 1.1. Self-contained 头文件 1.2. #define 保护 1.3. 前置声明 1.4. 内联函数 1.5. #include 的路径及顺序 2. 作用域 2. ...
- createFile
#include<iostream> #include<windows.h> #include<stdio.h> using namespace std; int ...
- Tr A(矩阵快速幂)
A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n < ...
- 2019 CCPC-Wannafly Winter Camp Day4(Div2, onsite)
slove 6/11 A.夺宝奇兵 Code:zz Thinking:zz 贪心即可.这条路线里,点n1和点n2肯定是相连的,接下来,点(n-1)1和点(n-1)2分别是和n1和点n2相连的,一共有两 ...
- 服务器重启后Jenkins项目部分丢失问题解决方法
1.进入webapps/jenkins/WEB-INF目录下,vi web.xml 2.修改 HUDSON_HOME下的value为/root/.jenkins 3.重启Jenkins:http:/ ...
- ptyhon技能树及其学习资源
GUI编程 tkinter Github项目 Tkinter by example effbot 文档 tkinter的一个designer,可以像在qtdesign那样创建UI文件 pyqt5 py ...
- 转 OGG 部署阶段常见问题
序号 问题 解决方案1 "2019-04-13 20:23:55 ERROR OGG-00868 Oracle GoldenGate Capture for Oracle, e_db1.pr ...
- Train Problem II(卡特兰数 组合数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023 Train Problem II Time Limit: 2000/1000 MS (Java/ ...
- 750的设计图以rem为单位的移动设备响应的设置大小
750的设计图,设置font-size: 125%; 1rem =20px; 大部分浏览器默认的字体大小为16px,谷歌默认字体大小为12px; 其他设备的fon-size的比列: 320/720 ...
- 源码安装caffe2时遇到的问题解决办法
https://github.com/facebookresearch/DensePose/issues/119