回文串---Best Reward

本文解析了一道关于字符串处理与算法优化的编程题——HDU 3613。该题要求将一串由26种不同类型的宝石组成的项链分割成两部分,使得这两部分作为回文串的价值之和最大。文章详细介绍了问题背景、输入输出格式、示例以及一种基于前缀和与后缀和的高效解法。
摘要由CSDN通过智能技术生成

HDU   3613

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: v  1, v  2, ..., v  26 (-100 ≤ v  i ≤ 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 v  1, the value of 'b' is v  2, ..., 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
题意:给了一个串由小写子字母组成,‘a'到’z'对应26种颜色的钻石,每种颜色的钻石对应有一个价值,将一个串分成两段,求这两个串的最大价值,规定若这个串是回文串,这这个串的价值是每个钻石价值相加,若不是钻石,则这个串价值为0;
 
思路:定义两个数组d1[]和d2[],d1[i]表示从第一颗钻石开始到第i颗钻石的价值和,d2[i]表示从 最后一颗钻石开始向左到第i颗钻石的价值和,然后从第一颗钻石到最后一颗钻石遍历,若以第i颗钻石位置切割,求出分割后的两串的价值和,将所有钻石遍历一次,求出最大值;
复杂度:O(n);
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=500005;
int v[30],p[2*N],d1[2*N],d2[2*N];
char s[2*N],str[2*N];
int n;

void kp()
{
    int mx=0;
    int id;
    ///for(i=n;str[i]!=0;i++)
    ///str[i]=0; ///没有这一句有问题,就过不了ural1297,比如数据:ababa aba;
    for(int i=1;i<n;i++)
    {
        if(mx>i)
            p[i]=min(p[2*id-i],p[id]+id-i);
        else
            p[i]=1;
        for( ;str[i+p[i]]==str[i-p[i]];p[i]++);
        if(p[i]+i>mx)
        {
            mx=p[i]+i;
            id=i;
        }
    }
}

void init()
{
    str[0]='$';
    str[1]='#';
    for(int i=0;i<n;i++)
    {
        str[i*2+2]=s[i];
        str[i*2+3]='#';
    }
    n=n*2+2;
    s[n]=0;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=1;i<=26;i++)
            scanf("%d",&v[i]);
        scanf("%s",s);
        n=strlen(s);
        init();
        kp();
        d1[0]=0;
        for(int i=1;i<n;i++)
        {
            if(str[i]=='#')  d1[i]=d1[i-1];
            else      d1[i]=d1[i-1]+v[str[i]-'a'+1];
        }
        d2[n]=0;
        for(int i=n-1;i>0;i--)
        {
            if(str[i]=='#')  d2[i]=d2[i+1];
            else      d2[i]=d2[i+1]+v[str[i]-'a'+1];
        }
        int sum,tmp=-99999999;
        for(int i=2;i<n-2;i=i+2)
        {
            sum=0;
            if(p[(i+2)/2]*2-1>=i+1) sum+=d1[i];
            if(p[(n+i)/2]*2-1>=n-i-1) sum+=d2[i+1];
            if(sum>tmp) tmp=sum;
        }
        printf("%d\n",tmp);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/chen9510/p/5426676.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值