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. Coursera Algorithms week3 快速排序 练习测验: Decimal dominants(寻找出现次数大于n/10的元素)

    题目原文: Decimal dominants. Given an array with n keys, design an algorithm to find all values that occ ...

  2. SpringBoot 启动定时任务

    再项目中大多会使用定时任务来定时执行一些操作,如:文件迁移,备份等等.今天就来跟大家一起学习下如何在SpringBoot中创建定时任务. 1.新建SpringBoot项目,或在原有的项目上添加(不知道 ...

  3. RegisterAttached 两种绑定方式

    RegisterAttached 含义:使用指定的属性名称.属性类型和所有者类型注册附加属性 绑定方式:C#绑定.WPF绑定 例:需求DataViewModel为DataView的VM层,在DataV ...

  4. php安全过滤类

    /*ansic码-Url码表: http://www.w3school.com.cn/tags/html_ref_urlencode.html ---------------------------- ...

  5. robotframework - 介绍&应用

    一.参考简书链接 :https://www.jianshu.com/p/c3a9d20db4e5 二.介绍 Robot Framework是一个基于Python的,可扩展的关键字驱动的测试自动化框架, ...

  6. Java中的管道流 PipedOutputStream和PipedInputStream

    我们在学习IO流的时候可能会学字节流.字符流等,但是关于管道流的相信大部分视频或者教程都是一语带过,第一个是因为这个东西在实际开发中用的也不是很多,但是学习无止境,存在既有理.JDK中既然有个类那说明 ...

  7. myeclipse配置tomcat后,无法正常使用的问题

    如图所示:一定要设置为Enable.否则部署tomcat时,没有tomcat8.0

  8. Coursera公开课-Machine_learing:编程作业2

    第三周编程作业:Logistic Regression 代码包在gitlab上:https://gitlab.com/luntai/Machine_Learning

  9. 网络中 ping 不通 路由表

    不管是在window还是在linux中,我们经常会遇到ping不通的问题. 这里的原因很多,比如不同的网段交换机做了一些限制等,这些问题是我们人工不能解决的. 但是,当你发现各自的网关是可以ping的 ...

  10. [Windows Server 2003] 安装IIS6.0及FTP

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装IIS6. ...