Codeforces Testing Round #14 (Unrated)
来源:互联网 发布:武则天和慈禧 知乎 编辑:程序博客网 时间:2024/05/19 21:14
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.
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.
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.
3abdeaj
47
5abcdefghijbdefaccbdg
136542
3aajjaa
44
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;}
- Codeforces Testing Round #14 (Unrated)
- Codeforces Testing Round #14 (Unrated)
- Codeforces Testing Round #14 (Unrated)
- Codeforces Testing Round #14 (Unrated) A
- Codeforces Testing Round #14 (Unrated) B
- Codeforces Testing Round #14 (Unrated) C. Minimum Sum
- Testing Round #14 (Unrated) A
- Testing Round #14 (Unrated) B
- Testing Round #14 (Unrated) B
- Testing Round #14 (Unrated) C
- Codeforces Testing Round #3
- Codeforces Testing Round #3
- Codeforces Testing Round #9
- 【Codeforces】Testing Round #12
- Codeforces Testing Round #10 C
- Codeforces Testing Round #5 B DFS
- CodeForces Testing Round #8 A.IQ Test
- Codeforces Testing Round #9 D/386D
- jquery操作样式
- bzoj2243 [SDOI2011]染色 树链剖分
- yii 基于php的Rbac权限控制
- thriftserver&beeline的使用
- noi-7912-笨小猴
- Codeforces Testing Round #14 (Unrated)
- 基于.NET平台常用的框架整理
- manacher&&后缀数组
- Android开发之漫漫长途 Ⅷ——Android Binder(也许是最容易理解的)
- MyCat是什么?
- 服务器端证件车牌识别api
- 数据结构实验之排序二:交换排序
- 如何判断自己是否具有成为一名优秀程序员的潜质
- putty远程连接虚拟机中ubuntu