题目链接:http://codeforces.com/problemset/problem/910/C
题目大意:将a~j分别赋值0~9,然后计算表达式的最小值
一道普通的模拟题,思路在于将a~j的权重计算清楚,然后排序分配0~9,同时注意一下前导零的问题。
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
char dic[1000][10];
int cnt_appear[15][10];
struct node
{
int index;
int rate;
};
int f(int x)
{
int res = 1;
for(int i = 1; i <= x; i ++)
{
res *=10;
}
return res;
}
map<char,int>mymap;
bool cmp(node n1, node n2)
{
return n1.rate > n2.rate;
}
node rating[15];
bool can_not_zero[15];
int main()
{
for(int i = 1; i <= 10; i ++)
{
rating[i].index = i;
}
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i ++)
{
scanf("%s", dic[i]);
can_not_zero[dic[i][0] - ‘a‘ + 1] = 1;
int temp_len = strlen(dic[i]);
for(int j = 0; j < temp_len; j ++)
{
rating[dic[i][j] - ‘a‘ + 1].rate += f(temp_len - j);
}
}
sort(rating + 1,rating + 11, cmp);
int flag = 0;
int rk = 1;
for(int i = 1; i <= 10; i ++)
{
if(flag == 0)
{
if(can_not_zero[rating[i].index] == 1)
{
char c = rating[i].index + ‘a‘ - 1;
mymap[c] = rk;
rk++;
}
else
{
char c = rating[i].index + ‘a‘ - 1;
mymap[c] = 0;
flag = 1;
}
}
else if(flag == 1)
{
char c = rating[i].index + ‘a‘ - 1;
mymap[c] = rk;
rk++;
}
}
long long sum = 0;
for(int i = 1; i <= n; i ++)
{
long long temp_sum = 0;
for(int j = 0; j < strlen(dic[i]); j ++)
{
temp_sum = temp_sum * 10 + mymap[dic[i][j]];
}
sum+=temp_sum;
}
printf("%lld\n",sum);
return 0;
}