很久不码代码了,正如辉哥所说,再做出来一个题AC的冲动还是有的,明显很久不写了手生了,写的很麻烦还错误率很高。
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
int main()
{
char in[2550],zong[2550000],head[25500];
int store[80][1500],first = 0,tiao = 0; //一共七种长度编码,最大能存127个值
for(int i = 0; i < 7; i++)
for(int j = 0; j < pow(2,i + 1) - 1; j++)
{
int temp = j,ji = 1;
store[i][j] = 0;
while(temp > 0)
{
store[i][j] += temp % 2*ji;
ji *= 10;
temp /= 2;
}
}
while(1)
{
if(gets(in) == NULL) {tiao = 1;goto loop;}
if(in[0] == '0' || in[0] == '1') strcat(zong,in); //编码文本
else //编码头
{
loop:
if(first)
{
int p = 0;
while(1) //读入字符长度,最大为7
{
int len = 0;
char temp[14];
temp[0] = zong[p++];
temp[1] = zong[p++];
temp[2] = zong[p++];
temp[3] = '\0';
for(int i = 0; i <= 2; i++)
if(temp[i] == '1') len += pow(2,2 - i);
if(len == 0) break;
while(1) //不断取对应长度的字符
{
memset(temp,0,sizeof(temp));
int shu,flag = 1;
for(int i = 0; i < len; i++)
{
if(zong[p] == '0') flag = 0;
temp[i] = zong[p++];
if(i == len - 1) temp[1 + i] = '\0';
}
if(flag) break;
sscanf(temp,"%d",&shu);
for(int j = 0; j < pow(2,len) - 1; j++)
if(shu == store[len - 1][j])
{
int limian = j + pow(2,len) - 1 - len;
printf("%c",head[limian]);
break;
}
}
}printf("\n");
}
first = 1;
memset(head,0,sizeof(head));
strcpy(head,in);
memset(zong,0,sizeof(zong));
if(tiao) break;
}
}
return 0;
}