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. js编码函数一些区别

    s对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1、 ...

  2. HTML5 ④

    块元素和行元素: 1.行元素:在一行内显示,不会自动换行的标签.不能设置宽高. 块元素:自动换行的标签,能设置宽高.*利于我们页面布局   比如:段落标签,标题标签都是块元素 2.两者可以互相转换,通 ...

  3. python全栈开发笔记---基本数据类型--数字型魔法

    数字  int a1 =123 a2=456 int 讲字符串转换为数字 a = " #字符串 b = int(a) #将字符串转换成整形 b = b + 1000 #只有整形的时候才可以进 ...

  4. [Leetcode 135]糖果分配 Candy

    [题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  5. 如何使用VSTO自动将Excel中的图表复制到Word

    如何使用VSTO自动将Excel中的图表复制到Word 原文地址:https://code.msdn.microsoft.com/How-to-copy-Chart-in-Excel-a29f9029 ...

  6. 进程中的Manager(),实现多进程的数据共享与传递

    __author__ = "Alex Li" from multiprocessing import Process, Managerimport osdef f(d, l): d ...

  7. Cracking The Coding Interview 9.6

    //原文: // // Given a matrix in which each row and each column is sorted, write a method to find an el ...

  8. mysql存储过程造数

    性能测试时,数据库表通常需要很多数据,此时我们可以用存储过程来造数,以下代码mysql.Oracle都可以用 首先,先查看数据库表的设计,可以看到每张表有多少字段,分别都是什么类型,哪个字段是自动增长 ...

  9. 基于UVM的verilog验证(转)

    reference:https://www.cnblogs.com/bettty/p/5285785.html Abstract 本文介绍UVM框架,并以crc7为例进行UVM的验证,最后指出常见的U ...

  10. 第三节 java 函数

    1.函数的定义 1.方法就是一段可重复调用的代码段 2.方法的格式1: 访问修饰符 返回值类型 方法名(参数类型 参数1,参数类型 参数2){ //权限访问修饰符 : public protected ...