Codeforces Testing Round #14 (Unrated) - C - Minimum Sum (排序)

C. 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].



题意:

有n个字符串,每个字符串都由'a'到'j'的字母组成,其中每一个字母代表一个数字,不同的字母代表不同的数字

其中n个字符串对应的数字不存在有前置零的情况

求将所有字符串相加得到的最小的和



数据比较小,直接将每个字母的权值存一下,然后排个序,还要标记一下能不能是0,权值大的字母对应的数字要小


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <utility>
#include <algorithm>
#define LL long long
using namespace std;
const int N = 1e3 + 10;

int T,n,m,x,y,z;
char s[N][10];
int zero[10];
LL val[10];
int vis[10];
pair<LL , int> p[10];
int main()
{
    scanf("%d",&n);
    LL ans = 0,cnt = 0;
    for(int i=0;i<n;i++){
        scanf("%s",s[i]);
        zero[s[i][0]-'a'] = 1;
        LL len = strlen(s[i]),base = 1;
        for(int j=len-1;j>=0;j--){
            val[s[i][j]-'a'] += base;
            if(!vis[s[i][j]-'a']) cnt++;
            vis[s[i][j]-'a'] = 1;
            base *= 10;
        }
    }
    for(int i=0;i<10;i++){
        p[i].first = val[i]; p[i].second = i;
    }
    sort(p,p+10);
    LL k = cnt;
    for(int i=0;k;i++){
        for(int j=9;j>=0;j--){
            if(i==0&&zero[p[j].second]) continue;
            if(p[j].first==0) continue;
            ans += i*p[j].first;
            p[j].first = 0;
            k--;
            break;
        }
    }
    printf("%lld\n",ans);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值