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

字符串合并处理

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

#include <iostream>
#include <string>
#include <set>
using namespace std;

int bitReverse(int num){
    if(num == 0){
        return 0;
    }
    int res = 0;
    int i = 0;
    while(i < 4){
        res <<= 1;
        res += num & 1;
        num >>= 1;
        ++i;
    }
    return res;
}

char processChar(char c){
    char res;
    int num = 0;
    if(c >= 'a' && c <= 'f'){
        num = bitReverse((int)c - 87);
    }
    else if(c >= 'A' && c <= 'F'){
        num = bitReverse((int)c - 55);
    }
    else if(c >= '0' && c <= '9'){
        num = bitReverse((int)c - 48);
    }
    else{
        return c;
    }

    if(num >= 0 && num <= 9){
        res = (char)num + '0';
    }
    if(num >= 10 && num <= 15){
        res = (char)num + '7';
    }
    return res;
}

int main(int argc, char* argv[]){
    string str1, str2;
    cin >> str1 >> str2;

    string str = str1 + str2;
    multiset<char> evens;
    for(int i = 0; i < str.size(); i += 2){
        evens.insert(str[i]);
    }
    multiset<char> odds;
    for(int i = 1; i < str.size(); i += 2){
        odds.insert(str[i]);
    }
    int turn = 0;
    string res;
    while(!evens.empty() && !odds.empty()){
        if(turn % 2 == 0){
            res.push_back(processChar(*evens.begin()));
            evens.erase(evens.begin());
        }
        else{
            res.push_back(processChar(*odds.begin()));
            odds.erase(odds.begin()); 
        }
        ++turn;
    }
    if(!evens.empty()){
        res.push_back(processChar(*evens.begin()));
        evens.erase(evens.begin());
    }
    if(!odds.empty()){
        res.push_back(processChar(*odds.begin()));
        odds.erase(odds.begin());        
    }
    cout << res << endl;
    return 0;
}
全部评论

相关推荐

03-04 07:14
门头沟学院 C++
黑皮白袜臭脚体育生:老板:都给工作机会了还想要工资,哪来这么多好事
点赞 评论 收藏
分享
在笔试的大西瓜很矫健:校招数分不用想了,这经历和学历都不够用,大厂更别想,初筛都过不了,说点不好听的小厂数分都进不去(小厂也是假数分),要两个对口实习+3个项目(或者3+2),而且要有含金量才能补一点你的学历劣势。 建议刷实习,社招找数分,校招看运气,能入行业就行,可以运营转数分
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务