csp 202305-3 解压缩(模拟)

 关键:实现16进制转2进制,以及16进制,2进制转10进制

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
string str, res;
int s;
int tran10(string str, int t) //t进制转10进制
{
    int res = 0;
    for(int i = 0; i < str.size(); i++)
    {
        if(isdigit(str[i])) res = res * t + (str[i] - '0');
        else res = res * t + (str[i] - 'a') + 10; //16进制
    }
    return res;
}

string tran2(char c1, char c2) //16进制转2进制
{
    string res = "00000000";
    int a = isdigit(c1) ? c1 - '0' : c1 - 'a' + 10; //低4位
    int b = isdigit(c2) ? c2 - '0' : c2 - 'a' + 10; //高4位
    for(int i = 3; i >= 0 && a; i--)
    {
        res[i] = a % 2 + '0';
        a /= 2;
    }
    for(int i = 7; i >= 4 && b; i--)
    {
        res[i] = b % 2 + '0';
        b /= 2;
    }
    return res;
}

int main()
{
    cin >> s; //字节数
    for(int i = 0; i < (s - 1)/8 + 1; i++) // s/8向上取整
    {
        string ss;
        cin >> ss;
        str += ss;
    }
    int i;
    for(i = 0; i < 2 * s; i += 2)
    {
        string ss = "";
        ss += str[i];
        ss += str[i + 1];
        if(tran10(ss, 16) < 128) //判断最高位是否为1,为1一定大于等于128
            break;
    }
    for(i += 2; i < 2 * s; i += 2)
    {
        string ss = tran2(str[i] , str[i + 1]);
        if(ss[6] == '0' && ss[7] == '0') //字面量
        {
            int len = tran10(ss.substr(0, 6), 2);
            if(len >= 60)
            {
                int idx = len - 59;
                string sss = "";
                for(int j = i + 2 * idx; j > i; j -= 2)
                {
                    sss += str[j];
                    sss += str[j + 1];
                }
                len = tran10(sss, 16);
                i += 2 * idx; //指针后移
            }
            int j, cnt;
            for(j = i + 2, cnt = 0; cnt < len + 1; j += 2, cnt++) //len为 l - 1
            {
                res += str[j];
                res += str[j + 1];
            }
            i = j - 2; //指针后移
        }
        else
        {
            int l, o;
            if(ss[6] == '0' && ss[7] == '1') //“01”回溯引用
            {
                l = tran10(ss.substr(3, 3), 2) + 4;

                ss = ss.substr(0, 3);
                ss += tran2(str[i + 2], str[i + 3]);
                o = tran10(ss, 2);

                i += 2; //指针后移
            }
            else //“10”回溯引用
            {
                l = tran10(ss.substr(0, 6), 2) + 1;
                ss = "";
                ss += str[i + 4]; ss += str[i + 5];
                ss += str[i + 2]; ss += str[i + 3];
                o = tran10(ss, 16);

                i += 4; //指针后移
            }
            int idx = res.size() / 2;

            for(int j = 2 * (idx - o), cnt = 0; cnt < l; j += 2, cnt++)
            {
                if(o < l && (j == 2 * idx)) //o < l 并且 回溯到结尾
                {
                    j = 2 * (idx - o);
                }
                res += res[j];
                res += res[j + 1];
            }
        }
    }
    for(int i = 0; i < res.size(); i++) //输出
    {
        if(i && i % 16 == 0)
        {
            cout << endl;
        }
        cout << res[i];
    }
    return 0;
}

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Patrickstarԅ(¯ㅂ¯ԅ)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值