HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)
题目链接:
https://cn.vjudge.net/problem/HDU-3613
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.
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算法求解一个串的前、后缀回文串标记数组)的更多相关文章
- HDU 3613 Best Reward(扩展KMP求前后缀回文串)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...
- 力扣算法:125-验证回文串,131-分割回文串---js
LC 125-验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 注:回文串是正着读和反着读都一样的字符串. ...
- HDU 3613 Best Reward(manacher求前、后缀回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...
- hdu 3613"Best Reward"(Manacher算法)
传送门 题意: 国王为了犒劳立下战功的大将军Li,决定奖给Li一串项链,这个项链一共包含26中珠子"a~z",每种珠子都有 相应的价值(-100~100),当某个项链可以构成回文时 ...
- 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 ...
- HDU 3613 Best Reward 正反两次扩展KMP
题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...
- HDU 5371(2015多校7)-Hotaru's problem(Manacher算法求回文串)
题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...
- 算法 -- 四种方法获取的最长“回文串”,并对时间复杂进行分析对比&PHP
https://blog.csdn.net/hongyuancao/article/details/82962382 “回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就 ...
- (最长回文串 模板) 最长回文 -- hdu -- 3068
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory ...
随机推荐
- JMS学习以及jms的实现activeMq
1.JMS规范介绍: http://www.cnblogs.com/hapjin/p/5431706.html http://elim.iteye.com/blog/1893038 http://bl ...
- 深入探讨 Java 类加载器(转)
原帖地址:https://www.ibm.com/developerworks/cn/java/j-lo-classloader/ 类加载器是 Java 语言的一个创新,也是 Java 语言流行的重要 ...
- uniGUI for C++ builder下如何利用FastReport实现数据记录本地打印
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/dlboy2018/article/details/81040260 (中行雷威2018.7.14于杭 ...
- cnblogs第一篇文章
大家好,以后我就在这里很多交流分享了!谢谢!
- 【图数据结构的遍历】java实现广度优先和深度优先遍历
[图数据结构的遍历]java实现广度优先和深度优先遍历 宽度优先搜索(BFS)遍历图需要使用队列queue数据结构: 深度优先搜索(DFS, Depth First Search)的实现 需要使用到栈 ...
- C# 子线程调用主线程窗体的解决方法
摘自其他人博客,自己试过确实解决问题.(如在自己定义的线程里面给textbox赋值) 由于Windows窗体控件本质上不是线程安全的.因此如果有两个或多个线程适度操作某一控件的状态(set value ...
- FluentAPI详细用法
设置主键 modelBuilder.Entity<x>().HasKey(t => t.Name); 设置联合主键 modelBuilder.Entity<x>().Ha ...
- 28_网络编程-TCP/UDP
一.传输层 1.定义 IP首部有一个协议字段,用来标识网络层(IP)的上一层所采用的是哪一种传输层协议.根据这个字段的协议号,就可以识别IP传输的数据部分究竟是TCP的内容还是 ...
- [ActionScript 3.0] 有必要记录一下:flash builder运用Animate CC 发布的swc的一个奇葩问题,亲测
之前一直用flash cs6 发布swc 配合flash builder4.6开发,最近用Animate CC发布swc,却出现无法flash builder4.6 无法连接到调试器的问题, 经过反复 ...
- centos7上mysql5.6版本主从复制
做主从复制实验: 第一步:主服务器上操作 1.修改主服务器master: [root@localhost ~]# vim /etc/my.cnf server_id = 1 //[必须]服务器唯一I ...