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?
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.
Print a single integer — the largest possible value of the resulting string DZY could get.
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
41
In the test sample DZY can obtain "abcbbc", value = 1·1 + 2·2 + 3·2 + 4·2 + 5·2 + 6·2 = 41.
为了写得顺手用了STL
AC code:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define fi first
#define se second
#define eps 1
const int maxn=1e4;
char s[maxn];
map<char,int> m;
int main() {
while(~scanf("%s",s+1)){
//printf("%d\n",strlen(s+1)); continue;
int k,maxv=-1;
scanf("%d",&k);
for(int i=1;i<=26;i++){
int v;
scanf("%d",&v);
m['a'+i-1]=v;
maxv=max(maxv,v);
}
int sum=0;
int len=strlen(s+1);
for(int i=1;i<=len;i++){
sum+=m[s[i]]*i;
}
for(int i=len+1;i<=len+k;i++){
sum+=maxv*i;
}
printf("%d\n",sum );
}
return 0;
}
本文介绍了一个关于字符串操作的问题——如何通过插入特定数量的小写字母来最大化字符串的价值。文章提供了完整的算法实现,包括输入字符串、插入字母的数量及各字母价值,并展示了如何计算最终优化后的字符串价值。
1145

被折叠的 条评论
为什么被折叠?



