Best Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 500    Accepted Submission(s): 201

Problem Description
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: v1, v2, ..., v26 (-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 v1, the value of 'b' is v2, ..., 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
 
题目大意:一个项链,切成两部分求价值最大。(如子串为回文串那么价值为每个字母的价值之和,其他则价值为0)
分析:求出并记录回文前缀跟回文后缀,线性扫一下求最大值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std; const int MAX=+;
char s1[MAX],s2[MAX];
int f[MAX],sum[MAX],val[];
bool per[MAX],pos[MAX]; void getFail(char *a,int len)
{
int i,j;
f[]=f[]=;
for(i=;i<len;i++)
{
j=f[i];
while(j && a[i]!=a[j]) j=f[j];
f[i+]=(a[i]==a[j]?j+:);
}
} int find(char *a,char *b,int len)
{
int i,j=;
for(i=;i<len;i++)
{
while(j && a[i]!=b[j]) j=f[j];
if(a[i]==b[j]) j++;
}
return j==len?f[j]:j;//必需要分成两部分,排除整个为回文串的情况
} int main()
{
int n,k,i,num,ans,len;
scanf("%d",&n);
while(n--)
{
for(i=;i<;i++)scanf("%d",val+i);
scanf("%s",s1);
len=strlen(s1);
for(i=;i<=len/;i++) s2[i]=s1[len-i-],s2[len-i-]=s1[i];
sum[]=val[s1[]-'a'];
for(i=;i<=len;i++) sum[i]=sum[i-]+val[s1[i-]-'a'];
getFail(s1,len);
k=find(s2,s1,len);//求s1最大前缀回文串位置
memset(per,false,sizeof(per));
while(k) per[k]=true,k=f[k];//标记所有前缀回文串
getFail(s2,len);
k=find(s1,s2,len);//求s1最大后缀回文串位置
memset(pos,false,sizeof(pos));
while(k) pos[k]=true,k=f[k];//标记所有后缀回文串
ans=-INF;
for(i=;i<len;++i)
{
num=;
if(per[i]) num+=sum[i];
if(pos[len-i])num+=sum[len]-sum[i];
if(num>ans)ans=num; }
printf("%d\n",ans);
}
return ;
}

hdu 3613 KMP算法扩展的更多相关文章

  1. hdu 4300 kmp算法扩展

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  3. hdu 1686 KMP算法

    题意: 求子串w在T中出现的次数. kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html #include <iostream> #inc ...

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

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

  5. HDU 2594 kmp算法变形

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  6. KMP算法模板&&扩展

    很不错的学习链接:https://blog.csdn.net/v_july_v/article/details/7041827 具体思路就看上面的链接就行了,这里只放几个常用的模板 问题描述: 给出字 ...

  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 正反两次扩展KMP

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

  9. HDU 3613 Best Reward(拓展KMP算法求解)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

随机推荐

  1. 10分钟搞懂toString和valueOf函数(详细版)

    首先要说明的是这两种方法是toPrimitive抽象操作里会经常用到的. 默认情况下,执行这个抽象操作时会先执行valueOf方法,如果返回的不是原始值,会继续执行toString方法,如果返回的还不 ...

  2. 【动态规划】51nod1780 完美序列

    巧妙的转化:f前两维大小开反TLE了一发…… 如果一个序列的相邻两项差的绝对值小于等于1,那么我们说这个序列是完美的. 给出一个有序数列A,求有多少种完美序列排序后和数列A相同. Input 第一行一 ...

  3. (74)zabbix第三方认证之http(nginx basic auth)

    HTTP Basic Auth认证方式,我们将在实例中使用nginx来演示,Apache也类似. zabbix认证配置 Administration>> Authentication,将h ...

  4. 动态规划:Codeforces Round #427 (Div. 2) C Star sky

    C. Star sky time limit per test2 seconds memory limit per test256 megabytes inputstandard input outp ...

  5. 设置eclipse中的${user}

    打开eclipse根目录找到eclipse.ini文件增加初始配置: -Duser.name=snzigod@hotmail.com 重启eclipse后${user}变量的值就变成了snzigod@ ...

  6. Splay的用法

    splay区间增减查询 #include<cstdio> #include<algorithm> using namespace std; ; const int INF = ...

  7. python面试题解析(网络编程与并发)

    1.答: 应用层 与其它计算机进行通讯的一个应用,它是对应应用程序的通信服务的.例如,一个没有通信功能的字处理程序就不能执行通信的代码,从事字处理工作的程序员也不关心OSI的第7层.但是,如果添加了一 ...

  8. php-数据库连接类

    <?php class DB{ var $host; var $user; var $pwd; var $dbname; var $conn; function DB($host,$user,$ ...

  9. 蓝桥杯Java输入输出相关

    转载自:http://blog.csdn.net/Chen_Tongsheng/article/details/53354169 一.注意点 1. 类名称必须采用public class Main方式 ...

  10. ogre3D学习基础10 -- 键盘控制与鼠标控制(直接控制)

    要实现键盘,鼠标对场景的控制,首先要帧监听,就是在每一帧的渲染前后对它进行操作.这里的操作没有用到缓冲区,只是简单的直接获取. 1.这些步骤和前面的一样,直接上代码,操作还是在createScene函 ...