CodeForces - 910C Minimum Sum(贪心)

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
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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值