Codeforces Testing Round #14 (Unrated)

来源:互联网 发布:武则天和慈禧 知乎 编辑:程序博客网 时间:2024/05/19 21:14

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
3abdeaj
output
47
input
5abcdefghijbdefaccbdg
output
136542
input
3aajjaa
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 longusing 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;}



原创粉丝点击