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 week4 基础标签表 练习测验:Check if a binary tree is a BST

    题目原文: Given a binary tree where each 

  2. Linux学习大纲

  3. P4049 [JSOI2007]合金

    传送门 我数学可能白学了-- 因为三个数加起来等于\(1\),那么只要用前两个数就能表示,那么就能把每一种金属看成一个二维向量.考虑只有两个向量的时候,设这两个向量为\(a,b\),那么一个向量\(c ...

  4. HyperLedger Fabric部署与链码解读

    1.Fabric简介 Fabric是超级账本中的一个项目,用以推进区块链技术.和其他区块链类似,它也有一个账本,使用智能合约,且是一个参与者可以分别管理自身交易的系统.它是一个联盟链.Fabric与其 ...

  5. win10 激活方法 (各版本)

    很多人都在找Win10专业版永久密钥,其实win10激活码不管版本新旧都是通用的,也就是说一个win10专业版key,可以同时激活windows10专业版1809.1803.1709.1703.160 ...

  6. scrapy 框架持久化存储

    1.基于终端的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表或字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. # 执行输出指定格式进行存储:将 ...

  7. ACM_他和她(最大生成树+最短路径)

    他和她 Time Limit: 2000/1000ms (Java/Others) Problem Description: 大二上学期刚过完,平时成绩不错的小V参加了一个小型编程比赛,遇到一道题,虽 ...

  8. Linux文件系统inode、block解释权限(三)

    利用文件系统的inode和block来分析文件(目录)的权限问题. 为什么读取一个文件还要看该文件路径所有目录的权限? 为什么目录的w权限具有删除文件的能力,而文件w权限不行. inode:记录文件的 ...

  9. 如何在Linuxt系统下运行maven项目

    如何在Linuxt系统下运行maven项目 我们知道现在利用MAVEN来管理JAVA项目是非常常见的.比如公司一般都有一个自己的MAVEN仓库,通过MAVEN仓库来解决我们的项目依赖,更加方便的构建项 ...

  10. c#仿照qq登录界面编辑框内容操作

    using System; using System.Drawing; using System.Windows.Forms; namespace 案例演示 { public partial clas ...