Lowest Common Ancestor(位运算)

题目链接: https://ac.nowcoder.com/acm/contest/12794/J
十六进制每位转化为四位二进制

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack> 
#include <cmath>
using namespace std;
#define ll long long

string a[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000","1001"};
string b[] = {"1010", "1011", "1100", "1101", "1110", "1111"};
int po[4] = {1,2,4,8};

int main() {
     ll cnt;
    cin >> cnt;
    for(int k = 1; k <= cnt; ++k) {
        string s1,s2;
        cin >> s1 >> s2;
        string ss1,ss2;
        for(int i = 0; i < s1.length(); ++i) {
            if(s1[i] >= '0' && s1[i] <= '9') {
                ss1 += a[s1[i] - '0'];
            }
            else if(s1[i] >= 'a') {
                ss1 += b[s1[i] - 'a'];
            }
        }
        for(int i = 0; i < s2.length(); ++i) {
            if(s2[i] >= '0' && s2[i] <= '9') {
                ss2 += a[s2[i] - '0'];
            }
            else if(s2[i] >= 'a') {
                ss2 += b[s2[i] - 'a'];
            }
        }
        //cout << ss1 << " " << ss2 << endl;
        int i = 0, j = 0;
        while(ss1[i] == '0') i++;
        while(ss2[j] == '0') j++;
        int t = i;
        while(i < ss1.length()-1 && j < ss2.length()-1 && ss1[i+1] == ss2[j+1]) {
            i++; j++;
        }
        string ans = ss1.substr(t,i-t+1);
        switch(ans.length() % 4){
            case 1:
                ans = "000" + ans;
                break;
            case 2:
                ans = "00" + ans;
                break;
            case 3:
                ans = "0" + ans;
                break;
            default:
                break;
        }
        cout << "Case #" << k << ": ";
        for(int i = 0; i < ans.length(); i += 4){
            int x = 0;
            int c = 3;
            for(int j = i; j < i + 4; j++){
                if(ans[j] == '1') {
                    x += po[c];
                }
                c--;
            }
            if(x < 10) cout << x;
            else cout << (char)(x - 10 + 'a');
        }
        cout << endl << endl;
    }
}

比赛时写爆了的代码 傻了傻了5555

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack> 
#include <cmath>
using namespace std;
#define ll long long

ll po[1005] = {0};
ll po2[10005] = {0};

ll change(string s) {
    ll n = s.length();
    ll ans = 0, t = 0;
    for(int i = 0; i < n; i++) {
        if(s[i] >= 'a') t = s[i] - 'a' + 10;
        else t = s[i] - '0';
        ans += t * po[n-i-1];
    }
    return ans;
} 

ll f(ll x) {
    ll cnt = 0;
    if((x & 1) == 0) x += 1; 
    while(x) {
        x >>= 1;
        cnt++;
    }
    return cnt;
}

string f2(ll x) {
    string s1;
    ll t = 1, _x = 0;
    while(x) {
        _x = x % 16;
        if(_x < 10) s1 += (char)(_x + '0');
        else s1 += (char)(_x - 10 + 'a');
        x /= 16;
    }
    string s2;
    for(int i = s1.length()-1; i >= 0; --i) {
        s2 += s1[i];
    }
    return s2;
}

int main() {
    ll cnt;
    cin >> cnt;
    po[0] = 1;
    for(int i = 1; i < 1005; ++i) {
        po[i] = po[i-1] * 16;
    }
    po2[0] = 1;
    for(int i = 1; i < 10005; ++i) {
        po2[i] = po2[i-1] * 2;
    }
    for(int k = 1; k <= cnt; ++k) {
        string s1,s2;
        cin >> s1 >> s2;
        ll a = change(s1), b = change(s2);
        //cout << a << " " << b << endl;
        ll _a = f(a), _b = f(b);
        if(_a > _b) {
            a /= po2[(_a-_b)];
        }
        else if(_b > _a) {
            b /= po2[(_b-_a)];
        }
        while(a != b) {
            a /= 2;
            b /= 2;
        }
        string ans = f2(a);
        cout << "Case #" << k << ": " << ans << endl << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值