hdu3613 Best Reward manacher+贪心+前缀和
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+贪心+前缀和的更多相关文章
- HDU3613 Best Reward —— Manacher算法 / 扩展KMP + 枚举
题目链接:https://vjudge.net/problem/HDU-3613 Best Reward Time Limit: 2000/1000 MS (Java/Others) Memor ...
- hdu3613 Best Reward 扩展kmp or O(n)求最大回文子串
/** 题目:hdu3613 Best Reward 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意:有一个字符串,把他切成两部分. 如果这部 ...
- 【贪心+前缀】C. Fountains
http://codeforces.com/contest/799/problem/C [题意] 有n做花园,有人有c个硬币,d个钻石 (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 ...
- 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 ...
- HDU-3613-Best Reward(Manacher, 前缀和)
链接: https://vjudge.net/problem/HDU-3613 题意: After an uphill battle, General Li won a great victory. ...
- hdu3613 Best Reward【Manacher】
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- [Manacher][HDU3613][Best Reward]
题意: 将一段字符串 分割成两个串 如果分割后的串为回文串,则该串的价值为所有字符的权值之和(字符的权值可能为负数),否则为0. 问如何分割,使得两个串权值之和最大 思路: 裸的: 枚举分割点,计算, ...
- BZOJ3790神奇项链——manacher+贪心
题目描述 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色.为了制作这个项链,小 H 购买了两个机器.第一个机器可以生成所有形式 ...
- 【BZOJ3790】神奇项链 Manacher+贪心
[BZOJ3790]神奇项链 Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色.为了制作这个项链,小 H ...
随机推荐
- web技术栈中不可或缺的Linux技术
Web技术最重要的载体便是服务器,服务器运行在公共的网络环境下,为广大的用户提供网页浏览.信息通讯.消息推送等服务,从最开始的硬件服务器到虚拟主机技术,再到虚拟化技术的出现和云概念的兴起,绝大部分都是 ...
- js 操作dom
childNodes 返回当前元素所有子元素的数组 parentNode 返回元素的父节点 document.createElement(tagName) 文档对象上的createElement方法可 ...
- fzu1977
题解: 和前两题差不多 只不过变成了有些一定走,有些不一定 代码: #include<cstdio> #include<cmath> #include<algorithm ...
- Mysql中contact、group_concat、concat_ws、repeat
一.CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. mysql> select concat('11','22',' ...
- Spring Boot学习笔记----POI(Excel导入导出)
业务:动态生成模板导出Excel,用户修改完再导入Excel. Spring boot + bootstrap + poi 1.添加Dependence <dependency> < ...
- 第二节 java流程控制(判断结构+选择结构)
Java的判断结构: 1.if(条件表达式){ 执行语句 }: 2.if(条件表达式){ 执行语句 }else{ 执行语句 } 3. if(条件表达式){ 执行语句 }else if(条件表达式){ ...
- String转换成Boolean类型
Boolean.valueOf()方法: public static Boolean valueOf(String s) { return toBoolean(s) ? TRUE : FALSE; } ...
- fread 不能读取最后一个数据块
今天遇到一个问题,fread()竟然不能读取文件中的最后一个数据块. 我定义了一个结构体: Persong { char name[10]; char phone[15]; } 以及两个函数: int ...
- git解决not a git repository
意思是说没有库,需要你创建 git init zzz zzz文件夹就会出现在你的项目中,里面就会有.git文件,将里面的.git剪切到与项目同一级中 关注微信小程序
- Non-technical Blog Recording on Day of Sep. 19th 2017 in Retrospection.
Unfortunately, I heard a resignation message verbally from ESU (her name is: Su Yi in Chinese, Su fr ...