DZY Loves Strings
2014-08-19 13:57
239 查看
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
Output
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;
}
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.
将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;
}
相关文章推荐
- Codeforces Round #FF (Div. 2):B. DZY Loves Strings
- B. DZY Loves Strings
- CF D. DZY Loves Strings(字符串hash 暴力)
- 447B. DZY Loves Strings
- Codeforces Round #FF (Div. 2/B)/Codeforces447B_ DZY Loves Strings(字符串处理)
- Codeforces Round #254 (Div. 1) D. DZY Loves Strings
- Codeforces Round #FF (Div. 2) B. DZY Loves Strings
- Codeforces Round #FF (Div. 2):B. DZY Loves Strings
- Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力
- Codeforces Round #FF (Div. 2)B. DZY Loves Strings
- B. DZY Loves Strings
- CodeForces 447B---DZY Loves Strings
- CF 447B(DZY Loves Strings-贪心)
- Codeforces Round #FF (Div. 2)B. DZY Loves Strings
- CF 447B(DZY Loves Strings-贪心)
- codeforces-447B-DZY Loves Strings
- codeforces 444D DZY Loves Strings
- Codeforces Round #FF (Div. 2) Problem B DZY Loves Strings
- BZOJ 3561 DZY Loves Math VI
- BZOJ3569 DZY Loves Chinese II