题目链接:CCF-CSP真题《202305-3 解压缩》思路+python,c++满分题解_ccf csp真题_Hulake_的博客-CSDN博客
#include <bits/stdc++.h>
using namespace std;
int s;
string total,tmp,cur,cur_2;
vector<string> buffer; //缓冲区,即答案数组
string t16_2(string a) //十六转二
{
string ans = "11111111";
int num1;
if(a[0]>='0' && a[0]<='9')
{
num1 = a[0] - '0';
}
else
{
num1 = a[0] - 'a' + 10;
}
for(int i = 0;i < 4;i++)
{
ans[i] = num1/pow(2,3-i) + '0';
num1 %= int(pow(2,3-i));
}
int num2;
if(a[1]>='0' && a[1]<='9')
{
num2 = a[1] - '0';
}
else
{
num2 = a[1] - 'a' + 10;
}
for(int i = 0;i < 4;i++)
{
ans[4+i] = num2/pow(2,3-i) + '0';
num2 %= int(pow(2,3-i));
}
return ans;
}
int t2_int(string a) //二转十
{
int ans = 0;
for(int i = a.length()-1;i >= 0;i--)
{
ans += (a[i]-'0')*pow(2,a.length()-1-i);
}
return ans;
}
int t16_int(string a) //十六转十
{
int num1;
if(a[0]>='0' && a[0]<='9')
{
num1 = a[0] - '0';
}
else
{
num1 = a[0] - 'a' + 10;
}
int num2;
if(a[1]>='0' && a[1]<='9')
{
num2 = a[1] - '0';
}
else
{
num2 = a[1] - 'a' + 10;
}
return num1*16+num2;
}
int main()
{
ios::sync_with_stdio(false);
cin >> s;
for(int i = 0;i < s/8+1;i++)
{
cin >> tmp;
total+=tmp;
}
long long j = 0;
while(t16_2(total.substr(j,2))[0] == '1') //跳过引导域
{
j+=2;
}
j+=2;
for(;j < total.length();j+=2)
{
cur = total.substr(j,2);
cur_2 = t16_2(cur);
if(cur_2.substr(6,2) == "00") //字面量
{
int l = 0;
if(t2_int(cur_2.substr(0,6)) < 60)
{
l = t2_int(cur_2.substr(0,6))+1;
for(int i = 0;i < l;i++)
{
j+=2;
buffer.push_back(total.substr(j,2));
}
}
else
{
int nex = t2_int(cur_2.substr(0,6)) - 59;//储存字面量长度的字节数
for(int i = 0;i < nex;i++)
{
j+=2;
l+= t16_int(total.substr(j,2))*pow(256,i);
}
l+=1;
for(int i = 0;i < l;i++)
{
j+=2;
buffer.push_back(total.substr(j,2));
}
}
}
else if(cur_2.substr(6,2) == "01") //回溯引用情况1
{
long long o = 0;
long long l = t2_int(cur_2.substr(3,3))+4;
string o_2 = cur_2.substr(0,3);
j+=2;
o_2 += t16_2(total.substr(j,2));
o = t2_int(o_2);
if(l <= o)
{
long long start = buffer.size()-o;
for(long long i = start;i <= start+l-1;i++)
{
buffer.push_back(buffer[i]);
}
}
else
{
long long chushu = l/o;
long long yushu = l%o;
long long start = buffer.size()-o;
for(long long k = 0;k < chushu;k++)
{
for(long long i = start;i <= start+o-1;i++)
{
buffer.push_back(buffer[i]);
}
}
for(long long i = start;i <= start+yushu-1;i++)
{
buffer.push_back(buffer[i]);
}
}
}
else //回溯引用情况2
{
long long o = 0;
long long l = t2_int(cur_2.substr(0,6))+1;
j+=2;
o += t16_int(total.substr(j,2));
j+=2;
o += t16_int(total.substr(j,2))*16;
if(l <= o)
{
long long start = buffer.size()-o;
for(long long i = start;i <= start+l-1;i++)
{
buffer.push_back(buffer[i]);
}
}
else
{
long long chushu = l/o;
long long yushu = l%o;
long long start = buffer.size()-o;
for(long long k = 0;k < chushu;k++)
{
for(long long i = start;i <= start+o-1;i++)
{
buffer.push_back(buffer[i]);
}
}
for(long long i = start;i <= start+yushu-1;i++)
{
buffer.push_back(buffer[i]);
}
}
}
}
long long cnt = 0;
for(auto it:buffer)
{
cout << it;
cnt++;
if(cnt%8 == 0)
{
cout << endl;
}
}
return 0;
}
按理说运行错误无非就是溢出,除0模0,但是找了一个小时没找到问题在哪QwQ。。。