题目链接:https://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.

Input
The 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.

Output
Output 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

题目大意:
给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串(从左往右或者从右往左读,都一样),那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0。问最多能获得多少价值?

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
#define ll long long
int v[27];
const int maxn=500005;
char str1[maxn];
char str2[maxn];
int sum[maxn];
int  nex[maxn];
int extend1[maxn];
int extend2[maxn];
void get_next(char str[])     ///套模板
{int i=0;int p;int len=strlen(str);while(str[i]==str[i+1]&&i+1<len){i++;}nex[1]=i;p=1;for(int i=2; i<len; i++){if(nex[i-p]+i<nex[p]+p){nex[i]=nex[i-p];}else{int  j=nex[p]+p-i;if(j<0) j=0;while(i+j<len&&str[j]==str[i+j]){j++;}nex[i]=j;p=i;}}
}void extendkmp(char s1[],char s2[],int extend[]) ///套模板
{int i=0;int p;int len1=strlen(s1);int len2=strlen(s2);get_next(s2);while(s1[i]==s2[i]&&i<len2&&i<len1){i++;}extend[0]=i;p=0;for(i=1; i<len1; i++){if(nex[i-p]+i<extend[p]+p){extend[i]=nex[i-p];}else{int  j=extend[p]+p-i;if(j<0) j=0;while(i+j<len1&&j<len2&&s1[j+i]==s2[j]){j++;}extend[i]=j;p=i;}}
}
///extend[i]表示为以字符串S1中以i为起点的后缀字符串和模式串S2的最长公共前缀长度.
int main()
{int t;scanf("%d",&t);while(t--){for(int i=0; i<26; i++){scanf("%d",&v[i]);}scanf("%s",str1);int len=strlen(str1);sum[0]=0;for(int i=0; i<len; i++){sum[i+1]=sum[i]+v[str1[i]-'a'];str2[i]=str1[len-1-i];     ///str2是str1的反转}str2[len]='\0';memset(nex,0,sizeof(nex));get_next(str1);extendkmp(str2,str1,extend1);memset(nex,0,sizeof(nex));get_next(str2);extendkmp(str1,str2,extend2);int ans=-0x3f3f3f3f;for(int i=1;i<len;i++){int tmp=0;if(extend1[i]+i==len){tmp+=sum[len-i];}int pos=len-i;if(pos+extend2[pos]==len){tmp+=sum[len]-sum[pos];}if(tmp>ans) ans=tmp;}printf("%d\n",ans);}
}

HDU 3613 Best Reward 扩展kmp算法(将一个字符串分成两个回文串)相关推荐

  1. python【力扣LeetCode算法题库】409-最长回文串(数学 计数器)

    最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字 ...

  2. 【数据结构与算法】之深入解析“分割回文串II”的求解思路与算法示例

    一.题目要求 给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文,返回符合要求的最少分割次数. 示例 1: 输入:s = "aab" 输出:1 解释:只需一次分割就 ...

  3. 【数据结构与算法】之深入解析“分割回文串”的求解思路与算法示例

    一.题目要求 给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文串,返回 s 所有可能的分割方案. 回文串是正着读和反着读都一样的字符串. 示例 1: 输入:s = "aab ...

  4. 疯子的算法总结(七) 字符串算法之 manacher 算法 O(N)解决回文串

    有点像DP的思想,写写就会做. #include<bits/stdc++.h> using namespace std; const int maxn=1e7+5; char a[maxn ...

  5. 最长回文串 马拉车算法 C++

    最长回文串 LeetCode 5.最长回文串 给你一个字符串 s,找到 s 中最长的回文子串. 示例 1: 输入:s = "babad" 输出:"bab" 解释 ...

  6. 【算法】双指针算法 ( 有效回文串 II )

    算法 系列博客 [算法]刷题范围建议 和 代码规范 [算法]复杂度理论 ( 时间复杂度 ) [字符串]最长回文子串 ( 蛮力算法 ) [字符串]最长回文子串 ( 中心线枚举算法 ) [字符串]最长回文 ...

  7. 【算法】双指针算法 ( 双指针算法分类 | 相向双指针 | 有效回文串 )

    文章目录 一.双指针算法分类 二.相向双指针示例 ( 有效回文串 ) 一.双指针算法分类 面试时经常遇到 限制算法复杂度为 O(n)O ( n )O(n) 的情况 , 就需要使用以下算法 : 双指针算 ...

  8. 1616: 最长回文串(马拉车算法)

    1616: 最长回文串 Time Limit: 1 Sec Memory Limit: 128 MB [Submit][Status][Web Board] Description 求一个字符串的最长 ...

  9. 怎么判断一个字符串的最长回文子串是否在头尾_每日一道算法题,让你的头脑更活跃(寻找最长回文子串)...

    前言 最近准备把算法慢慢的捡起来,所以准备日更一道算法题目,难度自然是由简入难,所以同学们可以每天都来看看小编的更新. 日更时间定在每晚20:00,希望大家多多关注啦. 昨天就欠更了,简直就是打脸.过 ...

最新文章

  1. linux ping 报错 sendmsg: Operation not permitted
  2. <LINUX内核完全剖析:基于0.12内核> 笔记一
  3. Iterator to list的三种方法
  4. JavaFX即将推出您附近的Android或iOS设备吗?
  5. WAS集群系列(5):集群搭建:步骤3:安装IHS软件
  6. java自动识别验证码_Java使用OCR技术识别验证码实现自动化登陆方法
  7. 自动生成宣传单打印页--提高工作效率
  8. MAX187_量程0-2.5伏电压表_软件滤波
  9. 吃瓜笔记 | 旷视研究院:被遮挡人脸区域检测的技术细节(PPT+视频)
  10. 「代码随想录」518. 零钱兑换 II 【动态规划】力扣详解!
  11. BUAA北京地铁乘坐线路查询
  12. 最小二乘法曲线拟合+C代码
  13. 简述计算机程序执行过程,计算机程序的执行过程
  14. 前后端交互之——AJAX提交
  15. Learned Motion Matching-动作生成算法
  16. Deep Crossing
  17. 调取创蓝253国际短信验证码-代码示例2
  18. ASCII码------C语言
  19. 【百度地图API】你看过房产地图吗?你知道房产标注是如何建立的吗?
  20. python爬虫入门—selenuim自动登录qq邮箱

热门文章

  1. 什么是Ajax 和 json
  2. linux用飞信发短信
  3. sim插拔识别时间_智能手机插拔寿命测试标准
  4. 如何将图片压缩到15k以下?教你一键压缩图片的大小
  5. 用于阿尔茨海默症分期早期检测的多模态深度学习模型
  6. #define宏加括号和不加括号的区别
  7. Unity3D-VR《静夜诗》2-凝视宝剑和书籍时出现提示文本信息
  8. gnu linux额外支持的运算符,《Shell编程从入门到精通》张昊-chap1-8
  9. Qt基于QuaZIP实现文件压缩/解压(Linux下)
  10. day27 MySQL 表的约束与数据库设计