哈曼夫编码

#include
#include
#include
int cs[1000000];
void kuaipai(int l,int r)
{
int x=cs[l],i=l,j=r;
if(l>=r)
return ;
while(i {
while(i j–;
cs[i]=cs[j];
while(i=x)
i++;
cs[j]=cs[i];
}
cs[i]=x;
kuaipai(l,i-1);
kuaipai(i+1,r);
}
int main()
{
char a[1000000],b[1000000];
int n,i,j,s,k,t1,t2,cs2[1000000],s2,sum;
while(scanf("%s",&a)!=EOF)
{
s=0;
n=strlen(a);
for(i=0;i {
if(s0)
{
b[s]=a[i];
cs[s]=1;
s++;
}
else
{
k=1;
for(j=0;j {
if(b[j]a[i])
{
k=0;
break;
}
}
if(k
0)
{
cs[j]++;
}
else if(k
1)
{
b[s]=a[i];
cs[s]=1;
s++;
}
}
}
kuaipai(0,s-1);
s2=0;
while(s-2>=0)
{
cs2[s2]=cs[s-1]+cs[s-2];
s=s-1;
cs[s-1]=cs2[s2];
kuaipai(0,s-1);
s2++;
}
sum=0;
for(i=0;i {
sum=sum+cs2[i];
}
printf("%d %d %.1lf\n",n8,sum,(n8)/(sum+0.0));
}
return 0;
}

你可能感兴趣的:(哈曼夫编码)