题目链接:

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. panda

    这个项目很有意思,麻雀虽小五脏俱全. 页面使用rem和media query来设置字体和元素宽高image居中需要用到position 后端mysql使用阿里云的rds:nodejs的mysql模块的 ...

  2. bootstrap阶段测验【答案】

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  3. 七种bond模式说明

    第一种模式:mod=0 ,即:(balance-rr) Round-robin policy(平衡抡循环策略) 特点:传输数据包顺序是依次传输(即:第1个包走eth0,下一个包就走eth1….一直循环 ...

  4. unigui编译路径设置

    unigui编译路径设置 先设路径变量 再追加如下路径,即可成功编译: ;$(uni)\uniTools\Dcu\Delphi2021;$(uni)\uniGUI\Dcu\Delphi2021;$(u ...

  5. IntelliJ IDEA通过maven构建ssm项目找不到mapper

    idea运行ssm项目的时候一直报错 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 原 ...

  6. 使用docker部署WordPress博客系统(win10企业版)

    docker介绍: docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相 ...

  7. solr 加载 停用/扩展词典

    startup.bat 停止词典的效果

  8. 【ElasticSearch】:QueryDSL

    Search API URI Search Response Body Search Query DSL Response Body Search使用Query DSL语句,相对URI Search功 ...

  9. 【Anaconda】:科学计算的Python发行版

    [背景] Python易用,但包管理和Python不同版本的问题比较头疼,特别是当你使用Windows的时候.为了解决这些问题,有不少发行版的Python,比如WinPython.Anaconda等, ...

  10. Python(28)---模块和包的基本概念

    一.模块 定义:在python中,一个 .py 文件就称为一个模块 使用模块的好处:最大的好处就是提高了代码的可维护性 分类(三种): python标准库 第三方模块 应用程序自定义模块 模块导入方法 ...