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.

题意:给出一个由26种字母组成的字符串,每种字母有权值,当一个串是回文串的时候,它的权值是其中所有字母的权值之和,否则是0。

现在要将这个字符串切成两段,问两段的权值和最大是多少。

用前缀和处理串的前缀权值和。

manacher进行回文串的匹配处理。

贪心切分位置并用manacher处理的数组判断是否是回文子串,并用前缀和数组求值,取最大值。

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn=5e5+;
char s[maxn],t[maxn<<];
int p[maxn<<];
int a[maxn],v[]; void manacher(){
int len=strlen(s),l=;
t[l++]='$';
t[l++]='#';
for(int i=;i<len;++i){
t[l++]=s[i];
t[l++]='#';
}
t[l]=;
int maxx=,num=;
for(int i=;i<l;++i){
p[i]=maxx>i?min(p[*num-i],maxx-i):;
while(t[i+p[i]]==t[i-p[i]])p[i]++;
if(i+p[i]>maxx){
maxx=i+p[i];
num=i;
}
}
} int main(){
int T;
scanf("%d",&T);
while(T--){
for(int i=;i<;++i)scanf("%d",&v[i]);
scanf("%s",s);
int l=strlen(s);
a[]=v[s[]-'a'];
for(int i=;i<l;++i){
a[i]=a[i-]+v[s[i]-'a'];
}
manacher();
int ans=;
for(int i=;i<l-;++i){
int tmp=;
int num=p[i+]-;
if(num==i+)tmp+=a[i];
num=p[i+l+]-;
if(num==l-i-)tmp+=a[l-]-a[i];
if(tmp>ans)ans=tmp;
}
printf("%d\n",ans);
}
return ;
}

hdu3613 Best Reward manacher+贪心+前缀和的更多相关文章

  1. HDU3613 Best Reward —— Manacher算法 / 扩展KMP + 枚举

    题目链接:https://vjudge.net/problem/HDU-3613 Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  2. hdu3613 Best Reward 扩展kmp or O(n)求最大回文子串

    /** 题目:hdu3613 Best Reward 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意:有一个字符串,把他切成两部分. 如果这部 ...

  3. 【贪心+前缀】C. Fountains

    http://codeforces.com/contest/799/problem/C [题意] 有n做花园,有人有c个硬币,d个钻石 (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 ...

  4. kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)

    After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...

  5. HDU-3613-Best Reward(Manacher, 前缀和)

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

  6. hdu3613 Best Reward【Manacher】

    Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. [Manacher][HDU3613][Best Reward]

    题意: 将一段字符串 分割成两个串 如果分割后的串为回文串,则该串的价值为所有字符的权值之和(字符的权值可能为负数),否则为0. 问如何分割,使得两个串权值之和最大 思路: 裸的: 枚举分割点,计算, ...

  8. BZOJ3790神奇项链——manacher+贪心

    题目描述 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色.为了制作这个项链,小 H 购买了两个机器.第一个机器可以生成所有形式 ...

  9. 【BZOJ3790】神奇项链 Manacher+贪心

    [BZOJ3790]神奇项链 Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色.为了制作这个项链,小 H ...

随机推荐

  1. uImage是什么

    vmlinux是内核文件,zImage是一般情况下默认的压缩内核映像文件,压缩vmlinux,加上一段解压启动代码得到.而uImage则是使用工具mkimage对普通的压缩内核映像文件(zImage) ...

  2. Ubuntu 16.04 中安装谷歌 Chrome 浏览器

    http://jingyan.baidu.com/article/335530da98061b19cb41c31d.html 根据教程安装成功!! http://askubuntu.com/quest ...

  3. java④

    1. 一元运算符:一个表达式就可以参与运算! * ++ -- * * 二元运算符:二个表达式就可以参与运算! * + / * - % * * 三元运算符:三个表达式就可以参与运算! * 数据类型 变量 ...

  4. Sql Server 中 根据列名查询表名

    已知列名 ELEMENT_ID ,查询所属表名称 Select O.name objectName, C.name ColumnName from sys.columns C inner join s ...

  5. 单元测试UI

    cnpm install -g --save mocha cnpm install  -g --save chai cnpm install -g --save istanbul const {sho ...

  6. easyui再学习的一部分代码

    <%-- Created by IntelliJ IDEA. User: zhen Date: // Time: : To change this template use File | Set ...

  7. leetcode第72题:编辑距离

    给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 1: 输 ...

  8. Android:如何获取屏幕的宽高

    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); DisplayMet ...

  9. CentOS部署PHP环境

    1.安装apache yum -y install httpd httpd-devel 2.启动apache systemctl start httpd.service 检查apache状态 syst ...

  10. python day03作业