当前位置:编程学习 > C/C++ >>

HDU3613:Best Reward

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
 
 
 
 
//拓展KMP,说实话,还是不太懂题目的意思
 
[cpp]  
#include <iostream>  
#include <cstdio>  
#include <cstring>  
using namespace std;  
  
const int MAXN = 500005;  
char S[MAXN],T[MAXN];  
int f[MAXN],extend1[MAXN],extend2[MAXN],val[30],sum[MAXN];  
  
void turn(char* s,char* t,int len)  
{  
    memset(t,0,sizeof(t));  
    for(int i = 0;i<len;i++)  
    t[i] = s[len-i-1];  
}  
  
void getnext(char* T,int* next)  
{  
    int len = strlen(T),a = 0;  
    next[0] = len;  
    while(a<len-1 && T[a] == T[a+1])  
    a++;  
    next[1] = a;  
    a = 1;  
    for(int k = 2;k<len;k++)  
    {  
        int p = a+next[a]-1,L = next[k-a];  
        if(k+L-1>=p)  
        {  
            int j = max(p-k+1,0);  
            while(k+j<len && T[k+j] == T[j])  
            j++;  
            next[k] = j;  
            a = k;  
        }  
        else  
        next[k] = L;  
    }  
}  
  
void EKMP(char* S,char* T,int* next,int* extend)  
{  
    getnext(T,next);  
    int slen = strlen(S),tlen = strlen(T),a = 0;  
    int minlen = min(slen,tlen);  
    while(a<minlen && S[a] == T[a])  
    a++;  
    extend[0] = a;  
    a = 0;  
    for(int k = 1;k<slen;++k)  
    {  
        int p = a+extend[a]-1,L = next[k-a];  
        if(k-1+L >= p)  
        {  
            int j = max(p-k+1,0);  
            while(k+j<slen && j<tlen && S[k+j] == T[j])  
            ++j;  
            extend[k] = j;  
            a = k;  
        }  
        else  
        extend[k] = L;  
    }  
}  
  
int main()  
{  
    int n,i,j;  
    cin >> n;  
    while(n--)  
    {  
        for(i = 0;i<26;i++)  
        cin >> val[i];  
        scanf("%s",S);  
        memset(sum,0,sizeof(sum));  
        for(i = 0;S[i];i++)  
        sum[i+1] = val[S[i]-'a'] + sum[i];  
        int len = strlen(S);  
        turn(S,T,strlen(S));  
        EKMP(S,T,f,extend2);  
        EKMP(T,S,f,extend1);  
        int max = -1000000000;  
        for(i = 0;i<len;i++)  
        {  
            if(i && extend1[i]+i == len)  
            {  
                int pos = extend1[i];  
                int tmp = sum[pos];  
                if(extend2[pos] + pos == len)  
                {  
                    tmp+=sum[len]-sum[pos];  
                }  
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,