CodeForces 447B---DZY Loves Strings

本文介绍了一种优化字符串值的算法,通过插入特定字符来最大化字符串的总值。包括输入解析、字符价值计算及最终值输出。

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

C - DZY Loves Strings
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

CodeForces 447B
Description
DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter c DZY knows its value wc. For each special string s = s1s2… s|s| (|s| is the length of the string) he represents its value with a function f(s), where

Now DZY has a string s. He wants to insert k lowercase letters into this string in order to get the largest possible value of the resulting string. Can you help him calculate the largest possible value he could get?

Input
The first line contains a single string s (1 ≤ |s| ≤ 103).

The second line contains a single integer k (0 ≤ k ≤ 103).

The third line contains twenty-six integers from wa to wz. Each such number is non-negative and doesn’t exceed 1000.

Output
Print a single integer — the largest possible value of the resulting string DZY could get.

Sample Input
Input
abc
3
1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Output
41
Hint
In the test sample DZY can obtain “abcbbc”, value = 1·1 + 2·2 + 3·2 + 4·2 + 5·2 + 6·2 = 41.

水题,直接做~~~

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int v[30];
int main()
{
    string a;
    cin>>a;
    int k;
    int sum=0; 
    scanf("%d",&k);
    int maxn;
    for(int i=1;i<=26;i++)
    {       
        cin>>v[i];
    }
    maxn=*max_element(v,v+26);
    int l=a.length();
    for(int i=1;i<=l;i++)
    {
        for(int j=1;j<=26;j++)
        {
            if((j-1)==a[i-1]-'a')
            {
                sum+=v[j]*i;
                break;
            }
        }
    }
    for(int i=l+1;i<=l+k;i++)
    {
        sum+=maxn*i;
    }
    cout<<sum<<endl;
    return 0;
}

仅代表个人观点,欢迎交流探讨,勿喷~~~
这里写图片描述

PhotoBy:WLOP

http://weibo.com/wlop

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值