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 ...
随机推荐
- 阿里云服务器 lnmp安装流程
nginx安装:wget http://nginx.org/download/nginx-1.12.2.tar.gztar zxvf nginx-1.12.2.tar.gzcd nginx-1.12. ...
- inxi 查看国产服务器的硬件配置信息
下载inxi 查看国产服务器的信息 https://codeload.github.com/smxi/inxi/tar.gz/3.0.35-1 注意查看命令是 ./inxi -F 2. 直接是二进制包 ...
- 小记--------maxwell启动失败解决
查看报错日志信息: com.github.shyiko.mysql.binlog.network.ServerException: Could not find first log file name ...
- 剑指offer6:旋转数组的最小数字
1. 题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一 ...
- mysql-tpcc测试
os: centos 7.4 db: mysql 5.7 software: tpcc-mysql TPC-C是专门针对联机交易处理系统(OLTP系统)的规范. tpcc-mysql是percona基 ...
- T100——q查询,子母查询(汇总——明细)练习笔记
范例: 代码: #add-point:input段落 name="ui_dialog.input" INPUT BY NAME g_master.bdate,g_master.ed ...
- go intall的使用
1.首先GOPATH路径指向src的上级目录 2.设置GOBIN路径指向bin目录 3.查看环境配置 4.go install 在src目录下 5.完成 6.pkg ide编译运行一下自动生成
- 19-Perl 特殊变量
1.Perl 特殊变量Perl 语言中定义了一些特殊的变量,通常以 $, @, 或 % 作为前缀,例如:$_.很多特殊的变量有一个很长的英文名,操作系统变量 $! 可以写为 $OS_ERROR.如果你 ...
- thymeleaf 模板使用 之 解决因HTML标签未闭合引起的错误
一.修改thymeleaf属性配置 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring ...
- Bootstrap3基础教程 03 导航栏
Bootstrap导航栏 创建一个默认的导航栏的步骤如下: 1.向 <nav> 标签添加 class .navbar..navbar-default. 2.向上面的元素添加 role=&q ...