HDU 3613 Best Reward

本博客探讨了一个问题,即如何通过切割一条由不同宝石组成的项链来获得最大的价值。详细介绍了Manacher算法在判断回文串方面的应用,以及如何通过遍历找到最佳切割点以实现价值最大化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/

基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。 智能教学辅助系统 这是一个智能教学辅助系统的前端项目,基于 Vue3+TypeScript 开发,使用 Ant Design Vue 作为 UI 组件库。 功能模块 用户模块 登录/注册功能,支持学生和教师角色 毛玻璃效果的登录界面 教师模块 备课与设计:根据课程大纲自动设计教学内容 考核内容生成:自动生成多样化考核题目及参考答案 学情数据分析:自动化检测学生答案,提供数据分析 学生模块 在线学习助手:结合教学内容解答问题 实时练习评测助手:生成随练题目并纠错 管理模块 用户管理:管理员/教师/学生等用户基本管理 课件资源管理:按学科列表管理教师备课资源 大屏概览:使用统计、效率指数、学习效果等 技术栈 Vue3 TypeScript Pinia 状态管理 Ant Design Vue 组件库 Axios 请求库 ByteMD 编辑器 ECharts 图表库 Monaco 编辑器 双主题支持(专业科技风/暗黑风) 开发指南 # 安装依赖 npm install # 启动开发服务器 npm run dev # 构建生产版本 npm run build 简介 本项目旨在开发一个基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值