思路

有点难的字符串题,细节比较多

一共需要3个数组,一个用于存储每行的原始字符串
一个用于对每个像素出现的次数进行计数
最后一个用于存贮前16个多的像素对应的char

再往后就是模拟了

细节上要注意的是从字符串中截取两个字符不要直接加,尽量用substr,如下

1
t = line.substr(i,2);

比如 find_if 的用法,如下:

1
2
3
auto it = find_if(b.begin(),b.end(),[&](const pair<string,int>& p){
return p.first == t;
});

再比如,将16进制字符串转化为10进制数,如下:

1
int x = stoll(t,nullptr,16);

总体难度:中位黄
实至名归的ccf四级题

代码

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;
}