DZY Loves Strings
来源:互联网 发布:海岛奇兵升级数据 编辑:程序博客网 时间:2024/05/01 16:53
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
abc31 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
Hint
In the test sample DZY can obtain "abcbbc", value = 1·1 + 2·2 + 3·2 + 4·2 + 5·2 + 6·2 = 41.
将k个字符插入到字符串中,使计算的值最大,由越往后排,坐标越大,因此只需将值较大的字符插到字符串的尾部即可。
代码写的略微有些繁琐,见谅。
#include<iostream>
#include<map>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
struct node{
char ch;
int num;
node(char a,int b){
ch=a;
num=b;
}
friend bool operator <(node a,node b){
if(a.num!=b.num)
return a.num<b.num;
else
return a.ch<b.ch;
}
};
map<char,int >mp;
vector<node >ve;
int k,sum;
string str;
void solve(){
sum=0;
int i;
for( i=0;i<str.length();i++){
sum=sum+mp[str[i]]*(i+1);
}
sort(ve.begin(),ve.end());
vector<node>::iterator it=ve.end();
it--;
i++;
while(k-->0){
sum=sum+(*it).num*i;
i++;
}
}
int main(){
char f;
int b;
while(cin>>str){
cin>>k;
mp.clear();
ve.clear();
for(f='a';f<='z';f++){
cin>>b;
mp[f]=b;
ve.push_back(node(f,b));
}
solve();
cout<<sum<<endl;
}
return 0;
}
- B. DZY Loves Strings
- B. DZY Loves Strings
- DZY Loves Strings
- 447B. DZY Loves Strings
- Codeforces 447B DZY Loves Strings
- CF 447B(DZY Loves Strings-贪心)
- codeforces-447B-DZY Loves Strings
- CodeForces 447B---DZY Loves Strings
- CodeForces 447B DZY Loves Strings
- CodeForces 447B DZY Loves Strings
- codeforces 444D DZY Loves Strings
- Codeforces 447B DZY Loves Strings
- CF 447B DZY Loves Strings(水题)
- Codeforces Round #FF (Div. 2) Problem B DZY Loves Strings
- Codeforces Round #FF (Div. 2):B. DZY Loves Strings
- Codeforces Round #FF (Div. 2) B. DZY Loves Strings
- Codeforces Round #FF (Div. 2) 447B DZY Loves Strings
- Codeforces Round #FF (Div. 2)B. DZY Loves Strings
- android ListView和GridView拖拽移位实现代码
- c#对象序列化
- Spring aop试用
- 设计模式C++实现(12)——备忘录模式
- Windows7 SP1导致含有ADO的程序运行失败问题
- DZY Loves Strings
- How to cache server responses in iOS apps
- ajaxanywhere 总结
- linux定时任务处理
- Android使用DexClassLoader 动态加载jar/apk
- 如何为自己的网站设置二级域名?
- [转载] 内存越界
- XMLHttpRequest状态status完整列表
- 设计模式C++实现(13)——中介者模式