HDOJ 3613 Best Reward

来源:互联网 发布:科比vs杜兰特交手数据 编辑:程序博客网 时间:2024/05/16 10:33

Best Reward

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


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
21 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1aba1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1acacac
 

Sample Output
16
 

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

题意:给出a~z这26个字母的价值,然后再给我们一个字符串,让我们把字符串分成两个子串,当子串是一个回文串的时候,它的价值就是所有其字母价值的和,如果不是回文串的话,那价值就是0,让我们输出获得最大的价值的方法的价值。

这题并不可怕,因为分离子串也是从中间剪成两段,并不会影响顺序,所以对于价值我们可以直接O(n)去打一个表,对回文串我们也可以进行O(n)Manacher算法求出p数组,然后再进行分割操作,接下来就只需要判断两边是否为回文串,然后把价值加起来判断是否是最大就好了。

#include <cstdio>#include <cstring> #include <iostream>#include <cstdlib>using namespace std;const int MAXN=550000;char str[MAXN*2];int p[MAXN*2];void Manacher(char s[],int len){    int l=0;    str[l++]='$';    str[l++]='#';    for(int i=0;i<len;i++){        str[l++]=s[i];        str[l++]='#';    }     str[l]=0;    int mx=0,id=0;    for(int i=0;i<l;i++){        p[i]=mx>i? min(p[2*id-i],mx-i):1;        while(str[i+p[i]] == str[i-p[i]]) p[i]++;        if(i+p[i] > mx){            mx = i+p[i];            id=i;        }     }}char s[MAXN];int sum[MAXN*2];int v[30];void input(){    for(int i=1; i<=26; i++)    scanf("%d",&v[i]);    scanf("%s",s);}void get_sum(){    int len=strlen(str);    for(int i=1;i<len;i++)    {        sum[i] = sum[i-1];        if('a'<=str[i] && str[i]<='z')        sum[i] += v[str[i]-'a'+1];//      printf("%d %d\n",i,sum[i]);    }}void solve(){    int len=strlen(s);    Manacher(s,len);    get_sum();    len=strlen(str);    int ans = 0;    for(int i=3;i<len-1;i=i+2)    {        int a1=0,a2=0;        if((1+i)/2+p[(2+(i-1))/2]-1 >= i-1)        a1 = sum[i]-sum[1];        if((len+i-1)/2-p[(len+i-1)/2]+1 <= i+1)        a2 = sum[len-2]-sum[i];        if(ans<a1+a2)        ans = a1+a2;    }    cout<<ans<<endl;}int main(){    int T;    cin>>T;    while(T--)    {        input();        solve();    }}


0 0
原创粉丝点击