地址:http://acm.hdu.edu.cn/showproblem.php?pid=3613

题目:

Best Reward

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

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
 
Source
 
Recommend
lcy

思路:扩展kmp

 #include<iostream>
#include<string>
#include<cstring>
#include<cstdio> using namespace std;
const int K=1e6+;
int nt[K],extand[K],v[],sum[K],l[K],r[K];
char sa[K],sb[K];
void Getnext(char *T,int *next)
{
int len=strlen(T),a=;
next[]=len;
while(a<len- && T[a]==T[a+]) a++;
next[]=a;
a=;
for(int k=; k<len; k++)
{
int p=a+next[a]-,L=next[k-a];
if( (k-)+L >= p)
{
int j = (p-k+)> ? (p-k+) : ;
while(k+j<len && T[k+j]==T[j]) j++;
next[k]=j;
a=k;
}
else
next[k]=L;
}
}
void GetExtand(char *S,char *T,int *next)
{
Getnext(T,next);
int slen=strlen(S),tlen=strlen(T),a=;
int MinLen = slen < tlen ? slen : tlen;
while(a<MinLen && S[a]==T[a]) a++;
extand[]=a;
a=;
for(int k=; k<slen; k++)
{
int p=a+extand[a]-, L=next[k-a];
if( (k-)+L >= p)
{
int j= (p-k+) > ? (p-k+) : ;
while(k+j<slen && j<tlen && S[k+j]==T[j]) j++;
extand[k]=j;
a=k;
}
else
extand[k]=L;
}
}
int main(void)
{
int t;cin>>t;
while(t--)
{
int ans=;
for(int i=;i<;i++)
scanf("%d",v+i);
scanf("%s",sa);
int len=strlen(sa);
for(int i=;i<len;i++)
sb[i]=sa[len-i-];
sum[]=v[sa[]-'a'];
for(int i=;i<len;i++)
sum[i]=sum[i-]+v[sa[i]-'a'];
sb[len]='\0';
GetExtand(sb,sa,nt);
for(int i=;i<len;i++)
if(extand[i]==len-i) l[len-i-]=;
else l[len-i-]=;
GetExtand(sa,sb,nt);
for(int i=;i<len;i++)
if(extand[i]==len-i) r[i]=;
else r[i]=;
for(int i=;i<len-;i++)
{
int tmp=;
if(l[i]) tmp+=sum[i];
if(r[i+]) tmp+=sum[len-]-sum[i];
ans=max(ans,tmp);
}
printf("%d\n",ans);
}
return ;
}

hu3613 Best Reward的更多相关文章

  1. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

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

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

  3. 回文串---Best Reward

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

  4. hdu 2647 Reward

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2647 Reward Description Dandelion's uncle is a boss o ...

  5. Reward

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. hdoj 2647 Reward【反向拓扑排序】

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  7. Reward HDU

    Reward                                    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32 ...

  8. Reward(拓扑结构+邻接表+队列)

    Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

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

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

随机推荐

  1. linux_shell_find命令

    使用find查找文件 基本格式:find path expression 1.按照文件名查找 (1)find / -name httpd.conf #在根目录下查找文件httpd.conf,表示在整个 ...

  2. 注解-->Spring配置

    有必要对JDK 5.0新增的注解(Annotation)技术进行简单的学习,因为Spring 支持@AspectJ,而@AspectJ本身就是基于JDK 5.0的注解技术.所以学习JDK 5.0的注解 ...

  3. Spring RestTemplate post

    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); map.add("auditPara ...

  4. 关于ie中实现弹性盒模型-我的css

    css3中的弹性盒模型大家都不陌生,但是能否在ie6中实现呢?第三方库中涉及到的页少之又少,也有一部分css框架中支持各种布局,下面给出我用的盒模型样式(为了以后copy方便而已): /******* ...

  5. ResNet 结构理解

    博客来源于:https://blog.csdn.net/buyi_shizi/article/details/53336192:https://blog.csdn.net/dcrmg/article/ ...

  6. iOS开发之-- textview 光标起始位置偏移

    使用textview的时候,会发生光标偏移的情况,其实是因为iOS7里导航栏,状态栏等有个边缘延伸的效果在. 把边缘延伸关掉就好了.代码如下 //取消iOS7的边缘延伸效果(例如导航栏,状态栏等等) ...

  7. 《C++ Primer Plus》第4章 学习笔记

    数组.结构和指针是C++的3中符合类型.数组可以在一个数据对象中存储多个同种类型的值.通过使用索引或下标,可以访问数组中各个元素.结构可以将多个不同类型的值存储在同一个数据对象中,可以使用成员关系运算 ...

  8. JSP内置对象——request 及其响应get和post请求的实例

    request对象客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例.request对象具有请求域,即完成客户端的 ...

  9. Vue基础-作用域插槽-列表组件

    Vue 测试版本:Vue.js v2.5.13 Vue 官网介绍作用域插槽时, 在 2.5.0+,slot-scope 能被用在任意元素或组件中而不再局限于 <template>. 作用域 ...

  10. 购物车删除商品,总价变化 innerHTML = ''并没有删除节点,内容仍存在

    w元素的上的下. function deleteLi(tmpId) { //document.getElementById(tmpId).innerHTML = ''; var wdel = docu ...