原题链接
题目太长了!这里就只放链接了
这道题目只要仔细阅读,把握好每一个细节,一步一步地做,实际是不难的,这也是csp第三题的特点:题目长、复杂,但是一般不涉及高级算法,难点在于能不能把每一个细节模拟出来。
对于c++,字符串有关的技巧和函数有超级多,比如char *的atoi,itoa,strcmp,strcat,strcpy。string的find和substr,stringstream的字符串分割方法。对于二进制还有很多位运算相关的技巧,对于十六进制-二进制-十进制三者的数字-字符串转换都是在csp第三题经常遇到的。这里就不做过多总结了,以后有时间的话,会专门出一期所有有关上述的总结!
下面是博主写的第三题题解,说实话,代码确实写的不太好,太冗余,大家有兴趣可以参考一下。
满分代码
#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
int s,h;
string has_print;//作用1:回溯 作用2:换行
int trans(char a,char b)//把二位十六进制数字符变成值
{
int num_a,num_b;
if(islower(a))num_a=a-'a'+10;
else num_a=a-'0';
if(islower(b))num_b=b-'a'+10;
else num_b=b-'0';
return num_a*16+num_b;
}
int main()
{
scanf("%d",&s);
if(s%8==0)h=s/8;
else h=s/8+1;
string str;
bool flag=false;//表示目前还是导引区
while(h--)
{
string temp;
cin>>temp;
str+=temp;
}
for(int i=0;i<str.length();i+=2)
{
int temp=trans(str[i],str[i+1]);
if(!flag)
{
if(temp>>7&1)continue;
else
{
flag=true;continue;
}
}
else
{
if(((temp&1)==0)&&((temp>>1&1)==0))
{
temp>>=2;
if(temp<=59)
{
int l=temp+1;
l*=2;
i+=2;
while(l--)
{
if(has_print.length()>0&&has_print.length()%16==0)
cout<<endl;
cout<<str[i];
has_print+=str[i];
i++;
}
i-=2;
}
else
{
temp-=59;
int cnt=0;
vector<int>len_num;
while(temp--)
{
i+=2;
len_num.push_back(trans(str[i],str[i+1]));
}
for(int j=len_num.size()-1;j>=0;j--)cnt=cnt*256+len_num[j];
cnt*=2;
cnt+=2;
i+=2;
while(cnt--)
{
if(has_print.length()>0&&has_print.length()%16==0)
cout<<endl;
cout<<str[i];
has_print+=str[i];
i++;
}
i-=2;
}
}
else if(((temp&1)==1)&&((temp>>1&1)==0))
{
temp>>=2;
int temp1=(temp>>3);
int o1=temp1;
temp1<<=3;
int l=temp-temp1;
l+=4;
i+=2;
int o2=trans(str[i],str[i+1]);
int o=(o1<<8)+o2;
l*=2;
o*=2;
int index=has_print.length()-o;
if(o>=l)
{
for(int j=index;j<index+l;j++)
{
if(has_print.length()>0&&has_print.length()%16==0)
cout<<endl;
cout<<has_print[j];
has_print+=has_print[j];
}
}
else
{
int d=l/o,r=l%o;
while(d--)
{
for(int j=index;j<index+o;j++)
{
if(has_print.length()>0&&has_print.length()%16==0)
cout<<endl;
cout<<has_print[j];
has_print+=has_print[j];
}
}
for(int j=index;j<index+r;j++)
{
if(has_print.length()>0&&has_print.length()%16==0)
cout<<endl;
cout<<has_print[j];
has_print+=has_print[j];
}
}
}
else if(((temp&1)==0)&&((temp>>1&1)==1))
{
temp>>=2;
int l=temp+1;
i+=2;
int b=trans(str[i],str[i+1]);
i+=2;
int a=trans(str[i],str[i+1]);
int o=a*256+b;
l*=2;
o*=2;
int index=has_print.length()-o;
if(o>=l)
{
for(int j=index;j<index+l;j++)
{
if(has_print.length()>0&&has_print.length()%16==0)
cout<<endl;
cout<<has_print[j];
has_print+=has_print[j];
}
}
else
{
int d=l/o,r=l%o;
while(d--)
{
for(int j=index;j<index+o;j++)
{
if(has_print.length()>0&&has_print.length()%16==0)
cout<<endl;
cout<<has_print[j];
has_print+=has_print[j];
}
}
for(int j=index;j<index+r;j++)
{
if(has_print.length()>0&&has_print.length()%16==0)
cout<<endl;
cout<<has_print[j];
has_print+=has_print[j];
}
}
}
}
}
return 0;
}