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 ≤ vi ≤ 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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define MAXN 500010
typedef long long LL;
/*
扩展KMP 算法
extend[i] 表示 从s[i]开始的s后缀 与t[]最长的相同前缀长度
*/
char str1[MAXN],str2[MAXN];
int sum[MAXN],v[],Next[MAXN],extend1[MAXN],extend2[MAXN];
void EKMP(char s[],char t[],int Next[],int extend[])
{
int i,j,p,L;//第一部分求Next[] Next[i]表示从i开始的后缀和t[]的最长相同前缀长度
int ls = strlen(s);
int lt = strlen(t);
Next[] = lt;
j = ;
while(j+<lt&&t[j]==t[j+])
j++;
Next[] = j;//t[1]开始的后缀和t[]的最长相同前缀长度 int a=;
for(i=;i<lt;i++)
{
p = a+Next[a]-;//a表示之前匹配的最长长度的开始,p表示之前匹配到的最长长度的结尾
L = Next[i-a];
if(i+L<p+)
Next[i] = L;
else
{
j = max(,p-i+);
while(i+j<lt&&t[i+j]==t[j])
j++;
Next[i] = j;
a = i;
}
} j=;
while(j<ls && j<lt && s[j]==t[j])j++;
extend[] = j;
a = ;
for(i=;i<ls;i++)
{
p = extend[a]+a-;
L = Next[i-a];
if(i+L<p+)
extend[i] = L;
else
{
j = max(,p-i+);
while(j+i<ls&&j<lt&&s[i+j]==t[j])
j++;
extend[i] = j;
a = i;
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
for(int i=;i<;i++)
scanf("%d",&v[i]);
scanf("%s",str1);
int len = strlen(str1);
sum[] = ;
for(int i=;i<len;i++)
{
sum[i+] = sum[i] + v[str1[i]-'a'];
str2[i] = str1[len--i];
} EKMP(str2,str1,Next,extend1);
EKMP(str1,str2,Next,extend2);
int ans = -;
for(int i=;i<len;i++)
{
int tmp = ;
if(extend1[i]+i==len)//前半部分回文
tmp += sum[len-i];
int pos = len-i;
if(extend2[pos]+pos==len)//后半部分回文
tmp += sum[len]-sum[pos];
ans = max(tmp,ans);
}
printf("%d\n",ans);
}
}

S - Best Reward 扩展KMP的更多相关文章

  1. hdu3613 Best Reward 扩展kmp or O(n)求最大回文子串

    /** 题目:hdu3613 Best Reward 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意:有一个字符串,把他切成两部分. 如果这部 ...

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

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

  3. 扩展KMP --- HDU 3613 Best Reward

    Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...

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

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

  5. HDU3613 Best Reward —— Manacher算法 / 扩展KMP + 枚举

    题目链接:https://vjudge.net/problem/HDU-3613 Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  6. [扩展KMP][HDU3613][Best Reward]

    题意: 将一段字符串 分割成两个串 如果分割后的串为回文串,则该串的价值为所有字符的权值之和(字符的权值可能为负数),否则为0. 问如何分割,使得两个串权值之和最大 思路: 首先了解扩展kmp 扩展K ...

  7. Best Reward 拓展kmp

    Problem Description After an uphill battle, General Li won a great victory. Now the head of state de ...

  8. 学习系列 - 马拉车&扩展KMP

    Manacher(马拉车)是一种求最长回文串的线性算法,复杂度O(n).网上对其介绍的资料已经挺多了的,请善用搜索引擎. 而扩展KMP说白了就是是求模式串和主串的每一个后缀的最长公共前缀[KMP更像是 ...

  9. KMP 、扩展KMP、Manacher算法 总结

    一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...

随机推荐

  1. PCB 利用nginx正向代理实现上网

    在PCB行业中,为了保证服务器的安全性,服务器正常都是需要与外网断开的,如果想在服务器通过浏览器下载一点东西是不行.通常作法是在一台可以上网的电脑下载文件,接着放到网络盘上,再从网络盘拷贝到服务器上. ...

  2. [Apple开发者帐户帮助]八、管理档案(4)

    您可以编辑,下载或删除在开发人员帐户中创建的配置文件.例如,如果您撤消了证书或禁用了配置文件中包含的设备,请编辑配置文件.或重新置备的个人资料,如果因为你它是无效的功能的应用程式服务. 注意: Xco ...

  3. [Swift通天遁地]三、手势与图表-(7)创建饼形图表并导出图表为PNG图片

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. windows下 redis/tomcat 服务自启动

    //设置redis服务自启动 //根据个人配置执行语句.   redis-server --service-install redis.windows.conf --loglevel verbose ...

  5. 常用mysql记录

    多个关键词 like$joinwhere .=" and CONCAT(`JpTel`,`JpName`) Like '%$keywords%' ";

  6. Jsp入门小常识

    因为选修了一门信息系统的课,选择了用jsp做了一个系统.在这期间自学了jsp的一点皮毛,特与大家分享: script标签:用于向jsp中嵌入java代码块,<%  // embed java c ...

  7. 了解MySQL的字符集

    在数据库中,字符乱码属于常见.多发问题.鉴于本人水平顶多只能归于不入流之类,写这篇文章时内心诚惶诚恐,实在担心误导大家.内容仅供参考,若有错误,请各位及时指出,我也好学习提高! MySQL的字符集有4 ...

  8. Spring.net(v1.3.2) 官网API-第一章 前言

    虽然有很好的工具和技术,但是开发软件应用仍然是很困难的.Spring为构建企业级应用提供了一个轻量级的解决方案,Spring提供了一个统一的.透明的方式去配置你的应用,和将AOP集成到你的软件中.Sp ...

  9. ionic2\ionic3 自定义弹窗

    ionic2及ionic3没有了popup及 其templateUrl属性 那我们如何对弹窗里加入自定义元素 从而达到自定义弹窗样式 那么就可以通过写h5页面来实现 自定义弹窗效果: 写个H5的弹窗及 ...

  10. css图片高清适配

    同一张图片,在普通屏显示正常,但高清屏出现模糊.原因是原来一个像素的点分成的四个像素的点进行了显示. 解决方案:在高清屏中把图片变成二倍图,前提是二倍的高清图已经存在. .icon{ backgrou ...