1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std;
bool cmp(const pair<string,int>& p,const pair<string,int>& q){ if(p.second == q.second){ return p.first < q.first; } return p.second > q.second; }
char dtoc(int x){ if(x > 9) return x - 10 + 'A'; return x + '0'; }
signed main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); int n; cin >> n; vector<string> a(n); cin.ignore(); vector<pair<string,int> >b; for(int i = 0 ; i < n ; i++){ string line; getline(cin,line); a[i] = line; for(int i = 0 ; i < line.size() ; i+=2){ string t = ""; t = line.substr(i,2); auto it = find_if(b.begin(),b.end(),[&](const pair<string,int>& p){ return p.first == t; }); if(it != b.end()){ it->second++; } else{ b.push_back({t,1}); } } } sort(b.begin(),b.end(),cmp); string re = ""; vector<pair<string,char> >c; for(int i = 0 ; i < 16 ;i++){ string t = b[i].first; c.push_back({t,dtoc(i)}); re += t; } vector<string> ans(n, ""); for(int i = 0 ; i< n ; i++){ string line = a[i]; for(int j = 0; j < line.size() ; j+=2){ string t = line.substr(j,2); bool f = false; for(int k = 0 ; k < c.size() ;k++){ if(t == c[k].first){ ans[i] += c[k].second; f = true; break; } } if(!f){ int minpos = 0; int minn = LLONG_MAX; int x = stoll(t,nullptr,16); for (int k = 0; k < c.size(); k++) { int y = stoll(c[k].first,nullptr,16); int d = abs(x - y); if (d < minn) { minn = min(minn, d); minpos = k; } } ans[i] += char(c[minpos].second); } } } cout << re << endl; for(int i = 0 ;i < n ; i++){ cout << ans[i] << endl; } return 0; }
|