Link
[Http://codeforces.com/group/1EzrFFyOc0/contest/910/problem/C]
Test instructions
Give you n strings, each character of a string is a~j, each character can be a substitution of 0~9, cannot have a leading zero
Ask you how to replace the sum of all the strings to minimize
Analysis
Count the number of occurrences of each character, such as Abc,a appeared 100 times, B appeared 10 times, C appeared 1 times. and whether the tag appears in the first place
The number of occurrences of the first order, but note with the selection of the mark is also followed, the last greedy is good, the specific look at the code
Code
#include <bits/stdc++.h>using namespace std; #define LL Long Longll Pow (ll x,ll y) {//fast power ll Ans=1; ll Base=x; while (y) {if (y&1) ans*=base; Base*=base; y>>=1; } return ans; int main () {Ios::sync_with_stdio (false); Cin.tie (0); Cout.tie (0); Freopen ("In.txt", "R", stdin); ll N,i,j; string S; ll b[12];//Mark if a character appears in the first place ll cnt[12]; while (cin>>n) {memset (cnt,0,sizeof (CNT)); Memset (b,0,sizeof (b)); for (i=0;i<n;i++) {cin>>s; ll Len=s.length (); b[s[0]-' a ']=1; for (j=len-1;j>=0;j--) cnt[s[j]-' a ']+=pow (10,len-1-j);//Count the occurrences of each character} for (i=0;i<9;i + +) {//select sort, as to whether to appear in the first position, also to transposition, in order to avoid having a leading 0 for (j=i+1;j<10;j++) {if (Cnt[j]>cnt[i]) { ll H; H=cnt[i];cnt[i]=cnt[j];cnt[j]=h; H=b[i];b[i]=b[j];b[j]=h; } } } ll sum=0; BOOL flag=1;//is used to mark whether 0 has been used for (i=0;i<10;i++) if (flag) {if (B[i]) sum+= (i+1) *cnt[i] ; else flag=0; } else sum+=i*cnt[i]; cout<<sum<<endl; } return 0;}
CF 910 C. Minimum Sum