#include <bits/stdc++.h>
using namespace std;
int index = 0;
typedef long long ll;
vector<int>vec;
string res;//存解压后的数据
string readByte(int number) {
char a[2 * number];
for (int i = 0; i < 2 * number; i++) {
cin >> a[i];
}
index += 2 * number;
return string(a, 2 * number);
}
void traceBack(int o, int l) {//有个坑,示例给的回溯是一个字节一位,题目实际是一个字节两位,要乘二
int start = res.length() - 2 * o ;
if (o >= l) {
res += res.substr(start, 2 * l);
} else {
string leftstring = res.substr(start, 2 * o);
int n = l / o;
int left = l % o;
while (n--)
res += leftstring;
res += leftstring.substr(0, 2 * left);
}
}
int main() {
int n;
cin >> n;
string lead, temp;
int vec_c;
while ((temp = readByte(1)) >= "80") { //读到最高位为0为止,最高位为0时必小于80
vec_c = stoi(temp, NULL, 16);
vec_c -= 128;
vec.push_back(vec_c);
}
vec_c = stoi(temp, NULL, 16);//这个temp最高位是0,直接放到vec,不必减最高位
vec.push_back(vec_c);
int length = 0; //原始数据长度
for (int i = 0; i < vec.size(); i++) {
length += vec[i] * pow(128, i);
}
while (index < 2 * n) {
temp = readByte(1); //字符串转成2进制数字转8位字符串再取低两位
string low2bits = bitset<8>(stoi(temp, NULL, 16)).to_string().substr(6, 2);
string high6bits = bitset<8>(stoi(temp, NULL, 16)).to_string().substr(0, 6);
if (low2bits == "00") {
ll literallength = stoi(high6bits, NULL, 2) + 1;
if (literallength > 60) { //l-1用小端序的1-4个字节表示储存于第一个字节的后面
literallength -= 60; //接下来的literallength位储存字面量原始字节长度
string string1 = readByte(literallength); //原顺序
string string2;
for (int i = string1.length() - 2; i >= 0; i -= 2)
string2 += string1.substr(i, 2);
literallength = stoi(string2, NULL, 16) + 1; //实际字面量长度
res += readByte(literallength);
} else if (literallength <= 60) { //随后的l字节即为字面量原始字节
res += readByte(literallength);
}
} else if (low2bits == "10") {
ll l = stoi(high6bits, NULL, 2) + 1;
string temp = readByte(2);
ll o = stoi(temp.substr(2, 2) + temp.substr(0, 2), NULL, 16);
traceBack(o, l);
} else if (low2bits == "01") {
string high3bits = high6bits.substr(0, 3);
ll l = stoi(high6bits.substr(3, 3), NULL, 2) + 4;
string nextbyte = bitset<8>(stoi(readByte(1), NULL, 16)).to_string();
ll o = stoi(high3bits + nextbyte, NULL, 2);
traceBack(o, l);
}
}
for (int i = 0; i < res.length(); i++) {
cout << res[i];
if ((i + 1) % 16 == 0)
cout << endl;
}
return 0;
}
CCF-CSP 202305-3 解压缩
最新推荐文章于 2024-07-24 22:13:06 发布