题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
#include <bits/stdc++.h>
using namespace std;
int main()
{
string line;
getline(cin, line);
size_t pos = line.find(' ');
string s1 = line.substr(0, pos);
string s2 = line.substr(pos + 1, line.size());
s1.append(s2);
vector<char> vodd;
vector<char> veven;
for(int i = 0; i < s1.size(); ++i) {
if(i % 2 == 0) veven.push_back(s1[i]);
else vodd.push_back(s1[i]);
}
sort(vodd.begin(), vodd.end());
sort(veven.begin(), veven.end());
string ss = "";
int i = 0;
for(; i < vodd.size() && i < veven.size(); ++i) {
ss.push_back(veven[i]);
ss.push_back(vodd[i]);
}
while(i < vodd.size()) ss.push_back(vodd[i++]);
while(i < veven.size()) ss.push_back(veven[i++]);
//0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
//1000 0100 1100 0010 1010 0110 1110 0001 1001 0101 1101 0011 1011 0111 1111
// 直接打出映射即可
string code = "123456789abcdefABCDEF";
string key = "84C2A6E195D3B7F5D3B7F";
string res = "";
for(int i = 0; i < ss.size(); ++i) {
if(code.find(ss[i]) != string::npos) {
res.push_back(key[code.find(ss[i])]);
} else res.push_back(ss[i]);
}
cout << res;
}

查看15道真题和解析