hdu 3613 Best Reward
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.
the number of test cases. The description of these test cases follows.
For each test case, the first line is 26 integers: v
1, v
2, ..., v
26 (-100 ≤ v
i ≤ 100, 1 ≤ i ≤ 26), represent the value of gemstones of each kind.
The second line of each test case is a string made up of charactor
'a' to 'z'. representing the necklace. Different charactor representing
different kinds of gemstones, and the value of 'a' is v
1, the value of 'b' is v
2, ..., and so on. The length of the string is no more than 500000.
OutputOutput a single Integer: the maximum value General Li can get from the necklace.Sample Input
2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
aba
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
acacac
Sample Output
1
6 题意是给出26个字母的value,然后给出一个字符串,把字符串分成两段,每段不为空,对于每段的value,如果字符串是回文就是各个字符value和,如果不是则为0,求最大value和。
这里用kmp算法协助判断回文串,首先判断s的前缀,把s反转加到s后面,变换后的s本身就是一个回文串了,然后一层一层往里找回文串,这样判断的最长相同前缀和后缀一定是回文串,因为他和自己反转后相同啊(回文串从中间分开,后半段是前半段的反转)。
由于s的长度翻倍了,所以对于切割的部位,应该原s串内,即在slen内,这里sum_value记录前缀是回文串的位置的sum(value前缀和),然后把原s反转一下,接下来就是找后缀了,同样的过程,只不过切割的位置不同,如果某个后缀是回文,切割的位置是 slen - 后缀位置(函数中flag为1的情况)。 代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std;
char s[];///字符串
int Next[];
int sum[];///value前缀和
int value[],slen;
int sum_value[];///记录从某位置切开后的value总和
int ans;
void getNext() {///确立Next数组
Next[] = -;
int len = slen * ;
int j = -,i = ;
while(i < len) {
if(j == - || s[i] == s[j]) Next[++ i] = ++ j;
else j = Next[j];
}
}
void check(int flag){
sum[] = ;
for(int i = ;i < slen;i ++)///计算前缀和
sum[i + ] = sum[i] + value[s[i] - 'a'];
reverse_copy(s,s + slen,s + slen);///把s反转添加到s后面
getNext();///确立此时的Next数组
int len = slen * ;
while(len) {
if(len < slen) {
int cut = len;
if(flag) cut = slen - len;
sum_value[cut] += sum[len];
ans = max(ans,sum_value[cut]);
}
len = Next[len];
}
}
int main() {
int T;
scanf("%d",&T);
while(T --) {
for(int i = ;i < ;i ++)
scanf("%d",&value[i]);
scanf("%s",s);///读入s
slen = strlen(s);
memset(sum_value,,sizeof(sum_value));
ans = ;
check();
reverse(s,s + slen);///反转一下
check();
printf("%d\n",ans);
}
}
先计算前缀,sumval[i]就代表从i后切开的value总和,manacher算法可以求得所有的回文子串,如果回文串是前缀,便在回文串长度下标对应位置加上前缀和,如果是后缀,便在后缀的前一位对应位置加上后缀的value和,如此找寻最大值。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#define MAX 500000
using namespace std;
int v[],sum[MAX + ];
char t[MAX * + ] = {'@','#'};
int p[MAX * + ];
int sumval[MAX + ];
int manacher(char *s,int len) {
int c = ,ans = ;
for(int i = ;i < len;i ++) {
t[c ++] = s[i];
t[c ++] = '#';
}
int rp = ,rrp = ;
for(int i = ;i < c;i ++) {
p[i] = i < rrp ? min(p[rp - (i - rp)],rrp - i) : ;
while(t[i + p[i]] == t[i - p[i]]) {
p[i] ++;
}
if(i - p[i] == ) sumval[(i + p[i] - ) / ] += sum[(i + p[i] - ) / ];
else if(i + p[i] == len * + ) sumval[(i - p[i]) / ] += sum[len] - sum[(i - p[i]) / ];
if(rrp < i + p[i]) {
rrp = i + p[i];
rp = i;
}
}
for(int i = ;i < len;i ++) {
ans = max(ans,sumval[i]);
}
return ans;
}
int main() {
int t;
char s[MAX + ];
scanf("%d",&t);
while(t --) {
for(int i = ;i < ;i ++) {
scanf("%d",&v[i]);
}
scanf("%s",s);
int len = strlen(s);
for(int i = ;i <= len;i ++) {
sum[i] = sum[i - ] + v[s[i - ] - 'a'];
sumval[i] = ;
}
printf("%d\n",manacher(s,len));
}
}
hdu 3613 Best Reward的更多相关文章
- HDU 3613 Best Reward 正反两次扩展KMP
题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...
- HDU - 3613 Best Reward(manacher或拓展kmp)
传送门:HDU - 3613 题意:给出26个字母的价值,然后给你一个字符串,把它分成两个字符串,字符串是回文串才算价值,求价值最大是多少. 题解:这个题可以用马拉车,也可以用拓展kmp. ①Mana ...
- 扩展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(扩展KMP求前后缀回文串)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...
- HDU 3613 Best Reward(manacher求前、后缀回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...
- HDU 3613 Best Reward(拓展KMP算法求解)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
- HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
- hdu 3613 Best Reward (manachar算法)
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Prob ...
随机推荐
- hdu5110 dp
题意 给 了 一 个 矩 阵 然 后 , 潜 艇 可 以 向 前 在 西北和东北之间 的区域, 然后每个潜艇有一个值D ,当到达潜艇距离为D的倍数的时候可以得到这个价值,这样我们1000*1000 的 ...
- 为什么要用Zero-Copy机制?
考虑这样一种常用的情形:你需要将静态内容(类似图片.文件)展示给用户.那么这个情形就意味着你需要先将静态内容从磁盘中拷贝出来放到一个内存buf中,然后将这个buf通过socket传输给用户,进而用户或 ...
- InFusion错误类型分析
1 God Class 1.1 特征 上帝类通常过多的操纵其他类的数据,从而破坏了类的封装性.上帝类从其他类中获得功能,同时增加了自身的耦合性,通常会导致自己具有规模过大和较高的复 ...
- express 项目前后台公用样式 /static/js/bootstrap.min.js
express 项目前后台公用样式 /static/js/bootstrap.min.js
- MySQL测试工具之-tpcc
首先安装tpcc 官网地址:https://github.com/Percona-Lab/tpcc-mysql [root@test3 src]# unzip tpcc-mysql-master.zi ...
- Django学习笔记之form组件的局部钩子和全局钩子
本文通过注册页面的form组件,查看其中使用的全局钩子和局部钩子. # Create your views here. class RegForm(forms.Form): username = fo ...
- 20145302张薇 《网络对抗技术》逆向及BOF基础实践
20145302张薇 <网络对抗技术>逆向及BOF基础实践 实验内容 实践对象:名为20145302的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单 ...
- Python学习札记(三十七) 面向对象编程 Object Oriented Program 8 @property
参考:@property NOTE 1.在绑定参数时,为了避免对属性不符合逻辑的操作,需要对传入的参数进行审核. #!/usr/bin/env python3 class MyClass(object ...
- 使用 Rcpp
正如我们所提到的那样,并行计算只有在每次迭代都是独立的情况下才可行,这样最终结果才不会依赖运行顺序.然而,并非所有任务都像这样理想.因此,并行计算可能会受到影响.那么怎样才能使算法快速运行,并且可以轻 ...
- 解决本地项目推送到码云(github),上提示:failed to push some refs to ...
本地项目上传github 命令如下: 1.git init 2.git add . 3.git commit -m "init" 4.git remote add origin ...