CodeForces - 910C Minimum Sum(贪心)
2018-01-15 17:18
1526 查看
Minimum Sum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Petya has n positive integers a1, a2, ..., an.
His friend Vasya decided to joke and replaced all digits in Petya's numbers with a letters. He used the lowercase letters of the Latin alphabet from 'a' to
'j' and replaced all digits 0 with one letter, all digits 1 with
another letter and so on. For any two different digits Vasya used distinct letters from 'a' to 'j'.
Your task is to restore Petya's numbers. The restored numbers should be positive integers without leading zeros. Since there can be multiple ways to do it, determine the minimum possible
sum of all Petya's numbers after the restoration. It is guaranteed that before Vasya's joke all Petya's numbers did not have leading zeros.
Input
The first line contains a single integer n (1 ≤ n ≤ 1 000)
— the number of Petya's numbers.
Each of the following lines contains non-empty string si consisting
of lowercase Latin letters from 'a' to 'j' — the Petya's
numbers after Vasya's joke. The length of each string does not exceed six characters.
Output
Determine the minimum sum of all Petya's numbers after the restoration. The restored numbers should be positive integers without
leading zeros. It is guaranteed that the correct restore (without leading zeros) exists for all given tests.
Examples
input
output
input
output
input
output
Note
In the first example, you need to replace the letter 'a' with the digit 1,
the letter 'b' with the digit 0, the letter
'd' with the digit 2, the letter 'e'
with the digit 3, and the letter 'j' with
the digit 4. So after the restoration numbers will look like [10, 23, 14].
The sum of them is equal to 47, which is the minimum possible sum of the numbers after the correct restoration.
In the second example the numbers after the restoration can look like: [120468, 3579, 2468, 10024, 3].
In the second example the numbers after the restoration can look like: [11, 22, 11].
题意:替换字母为数字,使得和最小。替换的数字不能有前导零。
解题思路:简单的贪心,最长的那个字符串的第一个字母肯定设为1.这样肯定是不行的。因为有可能次短的数量有很多,因此要设置一个权重。计算每一个字母的权重,然后按照权重设置数字,其中要注意前导零。判断下就好了。
#include <iostream>
#include <deque>
#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long int ll;
string a[1005];
vector<vector<string> > list(6);
int main(){
int N;
cin>>N;
map<char,int> w;//记录权重
map<char,int> nozero;//记录不能设置为0的字母
for(int i=0;i<N;i++){
cin>>a[i];
list[a[i].size()-1].push_back(a[i]);
for(int j=0;j<a[i].size();j++)
w[a[i][j]]+=pow(10,a[i].size()-j-1);//计算权重
nozero[a[i][0]]=1;
}
vector<pair<int,char> > s;
for(int i=0;i<10;i++)
if(w['a'+i]!=0)
s.push_back(make_pair(w['a'+i],'a'+i));//对权重排个序
sort(s.begin(),s.end());
map<char,int> r;//替换的数字
bool setz=1;
int num=1;
for(int i=s.size()-1;i>=0;i--){
//特判前导0
if(nozero[s[i].second]==0&&setz==1){
r[s[i].second]=0;
setz=0;
continue;
}
r[s[i].second]=num;
num++;
}
int sum=0;
for(int i=0;i<N;i++){
for(int j=0;j<a[i].size();j++)
a[i][j]='0'+r[a[i][j]];//替换
// cout<<a[i]<<endl;
sum+=stoi(a[i]);
}
cout<<sum<<endl;
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Petya has n positive integers a1, a2, ..., an.
His friend Vasya decided to joke and replaced all digits in Petya's numbers with a letters. He used the lowercase letters of the Latin alphabet from 'a' to
'j' and replaced all digits 0 with one letter, all digits 1 with
another letter and so on. For any two different digits Vasya used distinct letters from 'a' to 'j'.
Your task is to restore Petya's numbers. The restored numbers should be positive integers without leading zeros. Since there can be multiple ways to do it, determine the minimum possible
sum of all Petya's numbers after the restoration. It is guaranteed that before Vasya's joke all Petya's numbers did not have leading zeros.
Input
The first line contains a single integer n (1 ≤ n ≤ 1 000)
— the number of Petya's numbers.
Each of the following lines contains non-empty string si consisting
of lowercase Latin letters from 'a' to 'j' — the Petya's
numbers after Vasya's joke. The length of each string does not exceed six characters.
Output
Determine the minimum sum of all Petya's numbers after the restoration. The restored numbers should be positive integers without
leading zeros. It is guaranteed that the correct restore (without leading zeros) exists for all given tests.
Examples
input
3 ab de aj
output
47
input
5 abcdef ghij bdef accbd g
output
136542
input
3 aa jj aa
output
44
Note
In the first example, you need to replace the letter 'a' with the digit 1,
the letter 'b' with the digit 0, the letter
'd' with the digit 2, the letter 'e'
with the digit 3, and the letter 'j' with
the digit 4. So after the restoration numbers will look like [10, 23, 14].
The sum of them is equal to 47, which is the minimum possible sum of the numbers after the correct restoration.
In the second example the numbers after the restoration can look like: [120468, 3579, 2468, 10024, 3].
In the second example the numbers after the restoration can look like: [11, 22, 11].
题意:替换字母为数字,使得和最小。替换的数字不能有前导零。
解题思路:简单的贪心,最长的那个字符串的第一个字母肯定设为1.这样肯定是不行的。因为有可能次短的数量有很多,因此要设置一个权重。计算每一个字母的权重,然后按照权重设置数字,其中要注意前导零。判断下就好了。
#include <iostream>
#include <deque>
#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long int ll;
string a[1005];
vector<vector<string> > list(6);
int main(){
int N;
cin>>N;
map<char,int> w;//记录权重
map<char,int> nozero;//记录不能设置为0的字母
for(int i=0;i<N;i++){
cin>>a[i];
list[a[i].size()-1].push_back(a[i]);
for(int j=0;j<a[i].size();j++)
w[a[i][j]]+=pow(10,a[i].size()-j-1);//计算权重
nozero[a[i][0]]=1;
}
vector<pair<int,char> > s;
for(int i=0;i<10;i++)
if(w['a'+i]!=0)
s.push_back(make_pair(w['a'+i],'a'+i));//对权重排个序
sort(s.begin(),s.end());
map<char,int> r;//替换的数字
bool setz=1;
int num=1;
for(int i=s.size()-1;i>=0;i--){
//特判前导0
if(nozero[s[i].second]==0&&setz==1){
r[s[i].second]=0;
setz=0;
continue;
}
r[s[i].second]=num;
num++;
}
int sum=0;
for(int i=0;i<N;i++){
for(int j=0;j<a[i].size();j++)
a[i][j]='0'+r[a[i][j]];//替换
// cout<<a[i]<<endl;
sum+=stoi(a[i]);
}
cout<<sum<<endl;
return 0;
}
相关文章推荐
- Codeforces 489C - Given Length and Sum of Digits...(贪心)
- Codeforces 276C Little Girl and Maximum Sum【贪心】
- Codeforces #277.5 (Div. 2) C. Given Length and Sum of Digits...(简单贪心)
- CodeForces 489C (贪心) Given Length and Sum of Digits...
- codeforces 158B Taxi (贪心)
- codeforces_600C. Make Palindrome(贪心)
- 【Codeforces 733D】 Kostya the Sculptor【贪心】
- Codeforces 459E Pashmak and Graph(dp+贪心)
- CodeForces 230A Dragons(贪心)
- 【CodeForces 628C 】Bear and String Distance (贪心)
- CodeForces - 731C Socks(并查集)(贪心)
- Codeforces 500B New Year Permutation Floyd算法+贪心
- CodeForces 16B Burglar and Matches (贪心)
- Codeforces 854 C Planning(优先队列+贪心)
- CodeForces - 853A Planning(贪心)
- CodeForces 722D Generating Sets 贪心+Set
- codeforces 703D Mishka and Interesting sum (树状数组区间异或)
- CodeForces 614D 二分+贪心
- CodeForces 437D -- The Child and Zoo(并查集+贪心)
- CodeForces - 822D My pretty girl Noora(埃式筛素数+贪心)