1. 程式人生 > >CF910 C.Minimum Sum

CF910 C.Minimum Sum

sca div ret namespace spa urn emp sum ==

題目鏈接: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;
}

CF910 C.Minimum Sum