题解 | #字符串合并处理#

字符串合并处理

https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f

#include <algorithm>
#include <cctype>
#include <iostream>
#include <map>
#include <string>
using namespace std;

string& mySort(string &s) {
    string es, os;  // 偶序字串,奇序字串
    for(int i=0; i<s.size(); ++i) {
        if(i%2) os.push_back(s[i]);
        else es.push_back(s[i]);
    }
    sort(es.begin(), es.end());
    sort(os.begin(), os.end());
    for(int i=0; i<es.size(); ++i) {
        s[2*i] = es[i];
        s[2*i+1] = os[i];
    }
    return s;
}
/* 由于需要转换的字符不算多,所以提前建好字典 */
map<char, char> mmap = {
    {'0', '0'}, {'1', '8'}, {'2', '4'}, {'3', 'C'}, {'4', '2'}, {'5', 'A'}, {'6', '6'}, {'7', 'E'}, {'8', '1'}, {'9', '9'}, {'A', '5'}, {'B', 'D'}, {'C', '3'}, {'D', 'B'}, {'E', '7'}, {'F', 'F'}, {'a', '5'}, {'b', 'D'}, {'c', '3'}, {'d', 'B'}, {'e', '7'}, {'f', 'F'}
};
char transf(char &c) {
    if((c>='0' && c <= '9') || (c>='a' && c<='f') || (c>='A' && c <= 'F'))
        c = mmap[c];
    return c;
}

int main() {
    string str1, str2;
    cin >> str1 >> str2;
    string s = str1 + str2;
    mySort(s);
    for(char &c : s) transf(c);

    cout << s;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

04-27 08:59
常州大学 Java
牛客139242382号:《两门以上汇编语言》
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务