2019 byte beat [programming that] the largest map (greedy)

https://www.nowcoder.com/question/next?pid=1649268&qid=44705&tid=28244198

Setting a weight value for each letter v, the greater the weight should be set to a larger number, and then sorted by weight.

d [i] represents the letter i + 'A' corresponding to the number, e [i] denotes the i corresponding to the number of letters;

If there leading 0, find the first not pose a number of leading 0, then begin to exchange letters from this number.

 1 #define bug(x) cout<<#x<<" is "<<x<<endl
 2 #define IO std::ios::sync_with_stdio(0)
 3 #define ull unsigned long long
 4 #include <bits/stdc++.h>
 5 #define iter ::iterator
 6 #define pa pair<int,ll>
 7 #define pp pair<int,pa>
 8 using namespace  std;
 9 #define ll long long
10 #define mk make_pair
11 #define pb push_back
12 #define se second
13 #define fi first
14 #define ls o<<1
15 #define rs o<<1|1
16 ll mod=998244353;
17 const int N=2e5+5;
18 ll p[100];
19 ll a[100];
20 char s[100][100];
21 int T,n;
22 struct node{
23     int id;
24     ll v;
25 }b[100];
26 bool cmp(node n1,node n2){
27     return n1.v>n2.v;
28 }
29 int d[100],e[100];
30 int main(){
31     p[0]=1;
32     for(int i=1;i<=12;i++){
33         p[i]=p[i-1]*10;
34     }
35     scanf("%d",&T);
36     for(int z=1;z<=T;z++){
37         scanf("%s",s[z]+1);
38         n=strlen(s[z]+1);
39         int h=1;
40         for(int i=n;i>=1;i--){
41             int c=s[z][i]-'A';
42             a[c]+=p[h];
43             h++;
44         }
45     }
46     for(int i=0;i<10;i++){
47         b[i].id=i;
48         b[i].v=a[i];
49     }
50     sort(b,b+10,cmp);
51     for(int i=0;i<10;i++){
52         int k=b[i].id;
53         d[k]=9-i;
54         e[9-i]=k;
55     }
56     int id=0;
57     for(int g=0;g<10;g++){
58         int x=e[g];        
59         int f=0;
60         for(int i=1;i<=T;i++){
61             int c=s[i][1]-'A';
62             if(c==x){
63                 f=1;
64                 break;
65             }
66         }
67         if(!f){
68             id=g;
69             break;
70         }
71     }
72     for(int i=id;i>0;i--){
73         swap(e[i],e[i-1]);
74     }
75     for(int i=9;i>=0;i--){
76         d[e[i]]=i;
77     }
78     ll ans=0;
79     for(int i=1;i<=T;i++){
80         int n=strlen(s[i]+1);
81         ll h=1;
82         ll res=0;
83         for(int j=n;j>=1;j--){
84             int c=s[i][j]-'A';
85             c=d[c];
86             res+=1ll*c*h;
87             h*=10;
88         }
89         ans+=res;
90     }
91     printf("%lld\n",ans);
92 }

 

Guess you like

Origin www.cnblogs.com/ccsu-kid/p/11618885.html