题目链接:

https://cn.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.

InputThe first line of input is a single integer T (1 ≤ T ≤ 10) -
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个小写字母的价值,再输入一个字符串,问从哪个位置分割开,两个串的价值之和最大,当分成的串是回文串的时候,价值是各个字母的
和,不是回文串的时候,价值为0 解题思路
问题的关键是如何判断从哪个位置分割开后,前缀和后缀是不是回文串,普通的办法时间复杂度太高。
具体做法是将原s串反转赋值给t,拿s去匹配t,跑一遍kmp,得到s串的最大前缀子串的位置temp
同理拿t串去匹配s,跑一边kmp,得到s串的最大后缀子串的位置temp
由next数组的特性可知,将temp的位置标记为前缀回文后,寻找之前一个的前缀回文,也即next[temp]里面存的是之前一个前缀回文串的位置
,然后标记,如此循环,直到temp=0,表示没有前缀回文串,同理可得到后缀子串的回文标记数组。然后枚举每一个分割位置,判断是否时是
回文,计算求解最大值,其中利用前缀和数组加快计算。
*/
#include<cstdio>
#include<cstring> const int maxn = ;
const int inf=;
char s[maxn], t[maxn];
int val[], next[maxn], presum[maxn];
bool pre[maxn], pos[maxn]; void get_next(char b[], int len);
int kmp(char a[], char b[], int len); int main()
{
int T;
scanf("%d", &T);
while( T-- ){
for(int i = ; i < ; i++){
scanf("%d", &val[i]);
}
scanf("%s", s);
int len = strlen(s);
for(int i=;i<len;++i)
t[i] = s[len - - i],presum[i + ] = presum[i] + val[s[i] - 'a'];
t[len]='\0'; memset(next,,sizeof(next));
get_next(s,len);
int temp=kmp(s,t,len);//拿s去匹配t,也即t是主串,s是副串
memset(pre, , sizeof(pre));
while(temp){
pre[temp] = ;
temp = next[temp];
} memset(next,,sizeof(next));
get_next(t,len);
temp=kmp(t,s,len);
memset(pos, , sizeof(pos));
while(temp){
pos[temp] = ;
temp = next[temp];
} int ans = -inf;
for(int i = ; i < len; i++){
int sum = ;
if(pre[i])
sum += presum[i];
if(pos[len - i])
sum += presum[len] - presum[i];
if(sum > ans)
ans = sum;
}
printf("%d\n",ans);
}
return ;
} int kmp(char a[], char b[], int len){//注意a是副串,b是主串
int i=;
int j=;
while(i < len && j < len){
if(j == - || a[j] == b[i]){
i++;
j++;
}
else
j=next[j];
}
return j;//返回副串,也就是原串的最后匹配位置,故前j称为原串的最大前缀回文子串
} void get_next(char a[], int len){
int i = ;
int j = -;
next[] = -;
while(i < len){
if(j == - || a[i] == a[j]){
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
/*for(int i = 1; i <= len; i++){
printf("%d ",next[i]);
}
puts("");*/
}

HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)的更多相关文章

  1. HDU 3613 Best Reward(扩展KMP求前后缀回文串)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...

  2. 力扣算法:125-验证回文串,131-分割回文串---js

    LC 125-验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 注:回文串是正着读和反着读都一样的字符串. ...

  3. HDU 3613 Best Reward(manacher求前、后缀回文串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...

  4. hdu 3613"Best Reward"(Manacher算法)

    传送门 题意: 国王为了犒劳立下战功的大将军Li,决定奖给Li一串项链,这个项链一共包含26中珠子"a~z",每种珠子都有 相应的价值(-100~100),当某个项链可以构成回文时 ...

  5. Codeforces Round #410 (Div. 2) A. Mike and palindrome【判断能否只修改一个字符使其变成回文串】

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. HDU 3613 Best Reward 正反两次扩展KMP

    题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...

  7. HDU 5371(2015多校7)-Hotaru&#39;s problem(Manacher算法求回文串)

    题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...

  8. 算法 -- 四种方法获取的最长“回文串”,并对时间复杂进行分析对比&PHP

    https://blog.csdn.net/hongyuancao/article/details/82962382 “回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就 ...

  9. (最长回文串 模板) 最长回文 -- hdu -- 3068

    http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

随机推荐

  1. js反选

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  2. JBoss 系列四十九:JBoss 7/WildFly 中端口使用列表

    JBoss 7中端口使用列表 JBoss 7中所有配置都在一个文件中(standaone*.xml, domain.xml),和之前的JBoss相比JBoss 7用到的端口变少,我们将以表格的形式列出 ...

  3. Strust2总结

    1. JavaEE软件三层结构和MVC的区别? JavaEE软件三层机构是由sun公司提供JavaEE开发规范的:Web层(表现层).业务逻辑层.数据持久层.[其中WEB层会使用前端控制器模式] MV ...

  4. 201621123018《Java程序设计》第9周学习报告

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 泛型个人认为可以理解为一种模糊的类型,在里面写入各种方法,程序员可以根据需要再创建具体类型的对象,然后调用泛型 ...

  5. Python3.5 学习十七

    jQuery 模块=类库 jQuery就是DOM .BOM.Javascript的封装成的类库 一.查找元素.DOM只有10种左右选择器 jQuery有很多选择器和筛选器 PS:jQuery 推荐1系 ...

  6. lua 源码阅读顺序

    https://www.reddit.com/comments/63hth/ask_reddit_which_oss_codebases_out_there_are_so/c02pxbp Online ...

  7. WINDOWS平台下的栈溢出攻击从0到1(篇幅略长但非常值得一看)

    到1的这个过程.笔者也希望能够通过这些技术分享帮助更多的朋友走入到二进制安全的领域中.2.文章拓扑由于本篇文章的篇幅略长,所以笔者在这里放一个文章的拓扑,让大家能够在开始阅读文章之前对整个文章的体系架 ...

  8. 跟着刚哥深入学maven(通俗易懂)

    前言:目前所有的项目都在使用maven,可是一直没有时间去整理学习,这两天正好有时间,好好的整理一下. 一.为什么使用Maven这样的构建工具[why] ① 一个项目就是一个工程 如果项目非常庞大,就 ...

  9. POJ 2560

    #include<iostream> #include<algorithm> #include<cmath> #include<iomanip> #de ...

  10. easyui datebox 只选择月份的方法

    easyui datebox 只选择月份的方法 效果如下图: 代码如下: <html > <head> <meta charset="utf-8"&g ...