HDU-3613-Best Reward(Manacher, 前缀和)
链接:
https://vjudge.net/problem/HDU-3613
题意:
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.
思路:
跑一边马拉车, 对马拉车修改后的字符串跑前缀和.
枚举切割点即可.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 5e5+10;
int hw[MAXN*2], val[30], Sum[MAXN*2];
char s[MAXN], ss[MAXN*2];
void Manacher(char *str)
{
int maxr = 0, mid;
int len = strlen(str);
for (int i = 1;i < len;i++)
{
if (i < maxr)
hw[i] = min(hw[mid*2-i], maxr-i);
else
hw[i] = 1;
while (str[i+hw[i]] == str[i-hw[i]])
hw[i]++;
if (hw[i]+i > maxr)
{
maxr = hw[i]+i;
mid = i;
}
}
}
void Change(char *str, char *to)
{
to[0] = to[1] = '#';
int len = strlen(str);
for (int i = 0;i < len;i++)
{
to[i*2+2] = str[i];
to[i*2+3] = '#';
}
to[len*2+2] = 0;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
for (int i = 0;i < 26;i++)
scanf("%d", &val[i]);
scanf("%s", s);
Change(s, ss);
Manacher(ss);
int len = strlen(ss);
Sum[0] = Sum[1] = 0;
for (int i = 2;i < len;i++)
{
if (ss[i] == '#')
Sum[i] = Sum[i-1];
else
Sum[i] = Sum[i-1]+val[ss[i]-'a'];
}
int ans = -1e8;
for (int i = 3;i <= len-2;i += 2)
{
int lmid = (i+1)/2, rmid = (len-1+i)/2;
int tmp = 0;
if (hw[lmid] == lmid)
tmp += Sum[i];
if (hw[rmid]+rmid == len)
tmp += Sum[len-1]-Sum[i];
ans = max(ans, tmp);
}
printf("%d\n", ans);
}
return 0;
}
HDU-3613-Best Reward(Manacher, 前缀和)的更多相关文章
- hdu 3613"Best Reward"(Manacher算法)
传送门 题意: 国王为了犒劳立下战功的大将军Li,决定奖给Li一串项链,这个项链一共包含26中珠子"a~z",每种珠子都有 相应的价值(-100~100),当某个项链可以构成回文时 ...
- 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(manacher求前、后缀回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...
- Best Reward HDU - 3613(马拉车+枚举+前缀和)
题意: 给你一串字符串,每个字符都有一个权值,要求把这个字符串在某点分开,使之成为两个单独的字符串 如果这两个子串某一个是回文串,则权值为那一个串所有的字符权值和 若不是回文串,则权值为0 解析: 先 ...
- HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )
题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- HDU 3613 Best Reward(扩展KMP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3613 [题目大意] 一个字符串的价值定义为,当它是一个回文串的时候,价值为每个字符的价值的和,如果 ...
- hdu 3613 Best Reward
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
随机推荐
- python列表与字符串、元组的区别以及列表引用的方式
一.字符串 字符串也可以用下标取值.切片.for循环.len()取长度以及 in 和 not in 来进行操作. 但字符串是不可变的,不能被更改.只能构造一个“新的”字符串来存取你想要修改后的数据. ...
- Jboss: Using reverse path on top path: /xxx
环境 jboss 5.2 原因 加载资源的协议错误.一般在加载文件的时候,URL 都是以 file: 开头,但是在 jboss 上时,由于其虚拟化了路径,导致协议不一致,并且找不到外部的配置文件. 分 ...
- Spring4学习回顾之路03—XML配置Bean ,依赖注入的方式
配置Bean的形式可以基于XML文件的方式,也可以基于注解的方式,而Bean的配置方式可以通过全类名(反射),通过工厂方式和FactoryBean. XML形式 <?xml version=&q ...
- STL pair 常见用法详解
<算法笔记>学习笔记 pair 常见用法详解 //pair是一个很实用的"小玩意",当想要将两个元素绑在一起作为一个合成元素, //又不想因此定义结构体时,使用pair ...
- 【Life】 今天的思考
今天一个实习生来问我问题,他要用python操作outlook发送邮件,代码是从网上找的. 在其他人的电脑上可以成功运行,但在他的电脑上就失败. 处理过程 (1)我查看了他method里的代码, 发现 ...
- HTTP、HTTPS、WebSocket
一 .HTTP 1.1 HTTP发展史 1.1.1 什么是HTTP 超文本传输协议,是一个基于请求与响应,无状态的,应用层的协议,常基于TCP/IP协议传输数据,互联网上应用最为广泛的一种网络协议,所 ...
- Java EE javax.servlet中的ServletRequest接口
ServletRequest接口 public interface ServletRequest 子接口:HttpServletRequest 实现类:HttpServletRequestWrappe ...
- 装了vs2010 SP1后,开机速度慢
只要到服务里把 Microsoft .NET Framework NGEN v4.0.30319_X86 这个改成手动停止 或 禁用就可以 对vs没有影响 PS:禁了这个服务,开发wcf 在调试的 ...
- 11 Django实现WebSocket
因为需要实时显示状态的需求,想到了websocket,但是Django原生不支持websocket,后来搜索到了chango-channels项目,可以实现次需求. 一.Channels 官方文档 二 ...
- 将xml作为输入参数传递给存储过程
1 查看存储过程的方式 exec sp_helptext 'GetTrackingFeedLogByMaxId' select definition from sys.sql_modules wher ...