HDU 3613 Best Reward

Best Reward

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

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

题意是给定一个只含26个字母的权值,然后给定一个字符串,问如何将该字符串分成两块,且让这两块的字符串的权值和最大。一个字符串权值大小的规则是判断其是否是一个回文串,如果是权值就是其长度,如果不是,权值为0。

回文串可以考虑使用Manacher算法,对整个串进行回文串的判定,然后遍历,对于每个二分点,判断其两边是否是回文串并标记。最后再次遍历找到最大值。

代码如下:

/*************************************************************************
	> File Name: Best_Reward.cpp
	> Author: Zhanghaoran
	> Mail: chilumanxi@xiyoulinux.org
	> Created Time: 2016年02月24日 星期三 21时42分37秒
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;
const int MAXN = 500010;
int inde[30];
char str[MAXN];
char Ma[MAXN * 2];
int Mp[MAXN * 2];
int len;
long long value[MAXN];
bool flaga[MAXN];
bool flagb[MAXN];

void Manacher(){
    int l = 0;
    Ma[l ++] = '$';
    Ma[l ++] = '#';
    for(int i  = 0; i < len; i ++){
        Ma[l ++] = str[i];
        Ma[l ++] = '#';
    }
    Ma[l] = 0;
    int mx = 0, id = 0;
    for(int i = 0; i < l; i ++){
        Mp[i] = mx > i ? min(Mp[2 * id - i], mx - i) : 1;
        while(Ma[i + Mp[i]] == Ma[i - Mp[i]])
            Mp[i] ++;
        if(i + Mp[i] > mx){
            mx = i + Mp[i];
            id = i;
        }
        if(i - Mp[i] == 0)
            flaga[Mp[i] - 1] = true;
        if(i + Mp[i] == len + len + 2)
            flagb[Mp[i] - 1] = true;
    }
}

int T;

int main(void){
    cin >> T;
    while(T --){
        for(int i = 0; i < 26; i ++){
            cin >> inde[i];
        }
        memset(flaga, false, sizeof(flaga));
        memset(flagb, false, sizeof(flagb));
        cin >> str;
        len = strlen(str);
        for(int i = 1; i <= len; i ++){
            value[i] = value[i - 1] + inde[str[i - 1] - 'a'];
        }
        Manacher();
        long long ans = 0;
        long long temp = 0;
        for(int i = 1; i < len; i ++){
            if(flaga[i] == true)
                temp += value[i];
            if(flagb[len - i] == true)
                temp += value[len] - value[i];
            ans = ans > temp ? ans : temp;   
            temp = 0;
        }
        cout << ans << endl;

    }
}

 

查看原文:http://chilumanxi.org/2016/02/25/hdu-3613-best-reward/

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值