HDU3613 Best Reward

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

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

2010 ACM-ICPC Multi-University Training Contest(18)——Host by TJU

Recommend

lcy

将一个字符串从中间截断,若截成的小段是回文串,其得分等于每个字母的得分和;若不是回文串,该段得分为1

用manacher处理出每个点的回文半径。若截出的串中点的回文长度大于串长,则这是回文串。枚举断点记录最大得分即可。

 1 /**/
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<algorithm>
 7 using namespace std;
 8 const int mxn=1000200;
 9 char s[mxn];
10 char c[mxn];
11 int w[30];
12 int p[mxn],sum[mxn];
13 int len,ans=0;
14 void mana(){
15     int id=0,mx=0;
16     int i,j;
17     for(i=1;i<len;i++){
18         if(mx>i)p[i]=min(p[2*id-i],p[id]+id-i);
19         else p[i]=0;
20         while(s[i+p[i]]==s[i-p[i]])p[i]++;
21         if(p[i]+i>mx){
22             mx=p[i]+i;
23             id=i;
24         }
25     }
26     return;
27 }
28 int main(){
29     int T;
30     scanf("%d",&T);
31     int i,j;
32     while(T--){
33         for(i=0;i<26;i++)scanf("%d",&w[i]);
34         scanf("%s",c+1);
35         len=strlen(c+1);
36         sum[0]=0;
37         for(i=1;i<=len;i++)sum[i]=sum[i-1]+w[c[i]-‘a‘];
38         s[0]=‘$‘;s[1]=‘#‘;
39         for(i=1;i<=len;i++){s[i*2]=c[i];s[i*2+1]=‘#‘;}
40         len=len*2+2;s[len]=‘@‘;
41         mana();
42         ans=0;
43         len=(len-2)/2;
44         for(i=1;i<len;i++){
45             int tmp=0;
46             int l=(1+i*2+1)/2,r=(i*2+1+len*2+1)/2;
47             if(p[l]>=i*2+1-l) tmp+=sum[i];
48             if(p[r]>=r-(i*2+1))tmp+=sum[len]-sum[i];
49 //            printf("i:%d tmp:%d\n",i,tmp);
50             ans=max(ans,tmp);
51         }
52         printf("%d\n",ans);
53     }
54 }
时间: 2024-10-18 08:31:58

HDU3613 Best Reward的相关文章

hdu3613 Best Reward 扩展kmp or O(n)求最大回文子串

/** 题目:hdu3613 Best Reward 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意:有一个字符串,把他切成两部分. 如果这部分是回文串,那么他的值为所有字母的权值和.否则这部分值为0:这两部分的值和为该切法的权值. 求最大的切法的权值. 思路: 如果能够判断[0,i],[i,n-1]是一个回文串(0<=i<n)那么就可以枚举i,计算切割位置为i时候两部分的贡献和. 取最大的. 利用O(n)的算法求最长回文子串的做法获得

hdu-3613 Best Reward (manacher算法)

Best Reward 题目链接 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 gems

hdu3613 Best Reward【Manacher】

Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4345    Accepted Submission(s): 1791 Problem Description After an uphill battle, General Li won a great victory. Now the head of state

kuangbin专题十六 KMP&amp;&amp;扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)

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

manachar算法小结

1.hdu--4513 吉哥系列故事--完美队形II http://acm.hdu.edu.cn/showproblem.php?pid=4513 题意:中文题不解释 思路:数字型的manachar算法.将模板中的比较字符改为比较数字就行了. AC代码: 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <algorithm> 5 #include <cmat

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

题目链接: 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 differe

hdoj 2647 Reward

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5940    Accepted Submission(s): 1827 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he w

HDU2647 Reward(拓扑排序)反向建图

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4638    Accepted Submission(s): 1416 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he w

hdu 3613 Best Reward

Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)http://acm.hdu.edu.cn/showproblem.php?pid=3613 Problem Description After an uphill battle, General Li won a great victory. Now the head of state decide to