题目链接:

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

解题思路:

问题的关键还是如何判断从某个位置分割开后,前缀和后缀是否是回文串

这次采用拓展kmp算法求解,关于拓展kmp的介绍请参考另一篇博客:https://www.cnblogs.com/wenzhixin/p/9355480.html

  先将s1反转赋值给s2,用s1去匹配s2,得到s2中每个后缀与s1的最长公共前缀数组extpre

            用s2去匹配s1,得到s1中每个后缀与s2的最长公共前缀数组extpos

  再枚举每一个分割点,判断,求值,更新答案取最优。

关键是如何使用extend数组,举个例子来说

abcde

当分割长度为2的时候,需要分别判断ab和bcde是否是回文串

先看ab,要看ab是否是回文串需要用到extpre数组,为什么用到它而不是另一个分析如下:

用s2去匹配s1制作的到extpre数组

s2 edcba

s1 abcde

要判断ab是否是回文串,就是看ba与s1的最长公共前缀是多少,如果恰好是ba的长度,就说明ba在原串中存在。也即extpre[ls - i] == i(其中ls是串的总长度)。同时又有两者逆序,必然是回文串。

再看bcde是否是回文串

用s1去匹配s2制作的到extpos数组

s1 abcde

s2 edcba

要判断bcde是否是回文串,就要看bcde与s2的最长公共前缀是多少,如果恰好是dcde的长度,就说明bcde在反串中存在。也即extpre[i] == ls - i(其中ls是串的总长度)。同时又有两者逆序,必然是回文串。

这样就完成了,是否是回文串的判断。

代码实现:

 #include<cstdio>
#include<cstring> const int maxn = ;
int val[], presum[maxn], next[maxn], extpre[maxn], extpos[maxn];
char s1[maxn], s2[maxn]; void exkmp(char s[], char t[], int len, int ex[]);
void get_next(char t[], int len); int main()
{
int T;
scanf("%d", &T); while(T--){
for(int i = ; i < ; i++){
scanf("%d", &val[i]);
}
scanf("%s", s1);
int ls = strlen(s1);
presum[] = ;
for(int i = ;i < ls; i++){
s2[ls - - i] = s1[i];
presum[i + ] = presum[i] + val[s1[i] - 'a'];
}
s2[ls] = '\0'; exkmp(s2, s1, ls, extpre);//拿s1去匹配s2,得到s2中每个后缀与s1的最长公共前缀数组extpre
exkmp(s1, s2, ls, extpos);//拿s2去匹配s1,得到s1中每个后缀与s2的最长公共前缀数组extpos int ans = -;
for(int i = ; i <= ls - ; i++){//在长度为 i 处分割
int sum = ;
if(extpre[ls - i] == i)
sum += presum[i];
if(extpos[i] == ls - i)
sum += presum[ls] - presum[i];
if(sum > ans)
ans = sum;
}
printf("%d\n", ans);
}
return ;
} void get_next(char t[], int len){
next[] = len;
int k = ;
while(k < len && t[k] == t[k - ])
k++;
next[] = k; int po = ;
for(k = ; k< len; k++){
if(next[k - po] + k < next[po] + po)
next[k] = next[k - po];
else{
int j = next[po] + po - k;
if(j < )
j = ;
while(k + j < len && t[k] == t[k + j])
j++; next[k] = j;
po = k;
}
} } void exkmp(char s[], char t[], int len, int ex[])
{
memset(next, , sizeof(next));
get_next(t, len); int k=;
while(k < len && s[k] == t[k])
k++;
ex[] = k; int po = ;
for(k = ; k< len; k++){
if(next[k - po] + k < ex[po] + po)
ex[k] = next[k - po];
else{
int j = ex[po] + po - k;
if(j < )
j = ; while(k + j < len && j < len && s[k + j] == t[j])
j++;
ex[k] = j;
po = k;
}
}
}

HDU 3613 Best Reward(拓展KMP算法求解)的更多相关文章

  1. HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )

    题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...

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

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

  3. hdu 3613 Best Reward (manachar算法)

    Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Prob ...

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

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

  5. 拓展KMP算法详解

    拓展KMP解决的问题是给两个串S和T,长度分别是n和m,求S的每一个后缀子串与T的最长公共前缀分别是多少,记作extend数组,也就是说extend[i]表示S[i,n-1](i从0开始)和T的最长公 ...

  6. HDU - 3613 Best Reward(manacher或拓展kmp)

    传送门:HDU - 3613 题意:给出26个字母的价值,然后给你一个字符串,把它分成两个字符串,字符串是回文串才算价值,求价值最大是多少. 题解:这个题可以用马拉车,也可以用拓展kmp. ①Mana ...

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

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

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

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

  9. Best Reward 拓展kmp

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

随机推荐

  1. MySQL--Percona-XtraDB-Cluster使用xtrabackup来添加节点

    虽然PXC支持在线增加群集节点,但是目前尚未解决wsrep_sst_method=xtrabackup 或wsrep_sst_method=mysqldump时报错的问题,因此尝试手动完成xtraba ...

  2. 解决SHAREJPOINT 跨域问题

    目前仅支持IE7/8不支持IE11和谷歌 对于跨域情况,目前找到如果jquery是get获取方式,可以配置web.config相关属性,具体powershell命令如下: Add-PSSnapin M ...

  3. prometheus比zabbix好在哪点?

    分享网易云轻舟微服务选择基于 Prometheus 开发微服务监控系统的考量: 开源 云原生 与微服务监控需求的匹配度很高 开源 Prometheus是CNCF(云原生计算基金会)旗下成熟的开源项目, ...

  4. Shell命令之文本操作

    前言 在Linux中,文本处理操作是最常见的,应用非常广泛,如果能熟练掌握,可以大大提高开发效率. awk/sed/grep是文本操作领域的“三剑客”,学会了这3个命令就可以应对绝大多数文本处理场景. ...

  5. Windows打开应用,提示“此程序被组策略阻止”

    Windows打开应用,提示"此程序被组策略阻止",该问题为组策略限制了用户使用某个应用程序,一般可以在 1 控制面板--->管理工具--->本地安全策略-->软 ...

  6. Shell - 简明Shell入门07 - 数组(Array)

    示例脚本及注释 #!/bin/bash test0=() # 定义数组 test1=(a b c d e f) # 定义数组 test2=( # 定义数组 'A?' "BB!" C ...

  7. SQL 将一列多行数据合并为一行

    原表数据: 期望结果: 使用STUFF + FOR XML PATH即可实现以上效果 执行以下SQL: , , '') AS Course FROM Student AS T 可以看到输出结果与期望结 ...

  8. JS 跨域认识及如何解决

    什么是跨域 指的是浏览器不允许javascrip脚本向其他域名发起ajax请求. 跨域的各种情况判定 URL 说明 是否允许通信 http://www.a.com/a.js http://www.a. ...

  9. Docker端口映射(六)

    一.容器端口映射 1.1. 外部访问容器 在启动容器时候,如果不指定参数,在容器外部是无法通过网络来访问容器内的服务的 当容器运行一些网络服务的时候,我们可以通过指定-p或者-P参数来实现能够让外部访 ...

  10. 02-01 Java关键字、标识符、注释、常量和进制问题、变量和数据类型

    1:关键字 (1)被Java语言赋予特定含义的单词 (2)特点: 全部小写. (3)注意事项: A:goto和const作为保留字存在. B:类似于Notepad++这样的高级记事本会对关键字有特殊颜 ...