(HDU)1053 Entropy (哈夫曼树)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1053
#include
#include
#include
#include
using namespace std;
struct node
{
      int key;
      int f;
      int lson,rson;
      bool operator < (const node x)const
      {
            return x.key<key;
      }
};
node father;
node g[3100];
int sum=0;
void fuc(node x,int cnt);
int main()
{
      char str[1100];
      while(scanf("%s",str)!=EOF)
      {
            sum=0;
            priority_queue p;
            int len=strlen(str);
            if(len==3&&str[0]=='E'&&str[1]=='N'&&str[2]=='D')
            {
                  break;
            }

            int ii=0;
            for(int i=0;i
            {
                  int cnt=1;
                  char c;
                  if(str[i]!='#')
                  {
                        c=str[i];
                        str[i]='#';
                        for(int j=i+1;j
                        {
                              if(str[j]==c)
                              {
                                    cnt++;
                                    str[j]='#';
                              }
                        }
                        g[ii].f=ii;
                        g[ii].lson=g[ii].rson=-1;
                        g[ii].key=cnt;
                        p.push(g[ii]);
                        ii++;
                  }
            }

            if(ii==1)
            {
                  printf("%d %d 8.0\n",8*len,len);
            }
            else
            {
                  while(p.size()!=1)
                  {
                        node tmp1,tmp2;
                        tmp1=p.top();
                        p.pop();
                        tmp2=p.top();
                        p.pop();
                        g[ii].f=ii;
                        g[ii].lson=tmp1.f;
                        g[ii].rson=tmp2.f;
                        tmp1.f=ii;
                        tmp2.f=ii;
                        g[ii].key=tmp1.key+tmp2.key;
                        p.push(g[ii]);
                        ii++;
                  }
                  father=p.top();
                  p.pop();

                  fuc(father,0);
                  printf("%d %d %.1lf\n",len*8,sum,(double)(len*8)/sum);
            }
      }
      return 0;
}

void fuc(node x,int cnt)
{
      if(x.lson==-1&&x.rson==-1)
      {
            sum+=x.key*cnt;
            return ;
      }
      fuc(g[x.lson],cnt+1);
      fuc(g[x.rson],cnt+1);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值