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): 1074    Accepted Submission(s): 438


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
 
题目的意思就是给你一个项链,项链上有不同的宝石,然后把这个项链分成两部分,只有一个串是回文串的时候,项链上的宝石才有价值,问最后的最大价值问多少
我用的manacher,有的地方是借鉴了大牛的代码。。。。还是要加油。。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<cmath>
using namespace std;
const int maxn = 1000010;
char str[maxn];
int len[maxn], sum[maxn], v[27];
bool up[maxn], down[maxn];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        sum[0] = 0;
        memset(up, false, sizeof(up));
        memset(down, false, sizeof(down));
        for(int i = 0; i<26; i++)
            scanf("%d", &v[i]);
        scanf("%s", str);
        int l = strlen(str);
        for(int i = 1; i<=l; i++) sum[i] = sum[i - 1] + v[str[i-1] - 'a'];
        for(int i = l; i>=0; i--)
        {
            str[i*2+2] = str[i];
            str[i*2+1] = '#';
        }
        str[0] = '*';
        memset(len, 0, sizeof(len));
        int id = 0;
        for(int i = 1; i<l*2+2; i++)
        {
            if(len[id] + id > i)
                len[i] = min(len[id*2 - i], len[id] + id - i);
            else len[i] = 1;
            while(str[i + len[i]] == str[i - len[i]]) len[i]++;
            if(id + len[id] < i + len[i]) id = i;
            if(i - len[i] == 0) up[len[i] -1] = true;//前面len[i]-1个的子串为回文串
            if(i + len[i] == l * 2 + 2) down[len[i] - 1] = true;//后面len[i]-1个的子串为回文串
        }
        int ans = 0, temp;
        for(int i = 1; i<l; i++)
        {
            temp = 0;
            if(up[i]) temp += sum[i];
            if(down[l - i]) temp += sum[l] - sum[i];
            ans = max(ans, temp);
        }
        cout<<ans<<endl;
    }
    return 0;
}


资源下载链接为: https://pan.quark.cn/s/140386800631 通用大模型文本分类实践的基本原理是,借助大模型自身较强的理解和推理能力,在使用时需在prompt中明确分类任务目标,并详细解释每个类目概念,尤其要突出类目间的差别。 结合in-context learning思想,有效的prompt应包含分类任务介绍及细节、类目概念解释、每个类目对应的例子和待分类文本。但实际应用中,类目和样本较多易导致prompt过长,影响大模型推理效果,因此可先通过向量检索缩小范围,再由大模型做最终决策。 具体方案为:离线时提前配置好每个类目的概念及对应样本;在线时先对给定query进行向量召回,再将召回结果交给大模型决策。 该方法不更新任何模型参数,直接使用开源模型参数。其架构参考GPT-RE并结合相关实践改写,加入上下文学习以提高准确度,还使用BGE作为向量模型,K-BERT提取文本关键词,拼接召回的相似例子作为上下文输入大模型。 代码实现上,大模型用Qwen2-7B-Instruct,Embedding采用bge-base-zh-v1.5,向量库选择milvus。分类主函数的作用是在向量库中召回相似案例,拼接prompt后输入大模型。 结果方面,使用ICL时accuracy达0.94,比bert文本分类的0.98低0.04,错误类别6个,处理时添加“家居”类别,影响不大;不使用ICL时accuracy为0.88,错误58项,可能与未修改prompt有关。 优点是无需训练即可有较好结果,例子优质、类目界限清晰时效果更佳,适合围绕通用大模型api打造工具;缺点是上限不高,仅针对一个分类任务部署大模型不划算,推理速度慢,icl的token使用多,用收费api会有额外开销。 后续可优化的点是利用key-bert提取的关键词,因为核心词语有时比语意更重要。 参考资料包括
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值