[數(shù)據(jù)結(jié)構(gòu)]計(jì)算WPL 解題報(bào)告

Problem Description

Huffman編碼是通信系統(tǒng)中常用的一種不等長編碼,它的特點(diǎn)是:能夠使編碼之后的電文長度最短。


輸入:

第一行為要編碼的符號數(shù)量n
第二行~第n+1行為每個(gè)符號出現(xiàn)的頻率

輸出:

對應(yīng)哈夫曼樹的帶權(quán)路徑長度WPL


測試輸入

5
7
5
2
4
9

測試輸出

WPL=60

AcCode

//
//  main.cpp
//  計(jì)算WPL
//
//  Created by jetviper on 2017/3/26.
//  Copyright ? 2017年 jetviper. All rights reserved.
//

#include <stdio.h>
#define true 1
#define false 0
typedef  struct {
    unsigned int weight;
    unsigned int parent,leftChild,rightChild;
}HTNode, *HuffmnTree;

HTNode hufmanTree[100000];
int main() {
    
    int num,m;
    scanf("%d",&num);
    m = 2 * num -1;
    for(int i=1;i<=num;i++){
        scanf("%d",&hufmanTree[i].weight);
        hufmanTree[i].parent = 0;
        hufmanTree[i].leftChild = 0;
        hufmanTree[i].rightChild = 0;
    }
    int s1,s2,max1,max2,iset1,iset2;
    
    for(int i=num+1;i<=m;i++){
        max1 =0,max2=0;
        iset1 =0,iset2=0;
        for(int j=1;j<i;j++){
            if(hufmanTree[j].parent == 0){
                if(iset1 == 0){
                    max1 = hufmanTree[j].weight;
                    s1 = j;
                    iset1 = 1;
                    continue;
                }
                if(max1>hufmanTree[j].weight){
                    max1 = hufmanTree[j].weight;
                    s1 = j;
                    
                }
                
            }
        }
        for(int j =1;j<i;j++){
            if(j == s1)continue;
            if(hufmanTree[j].parent == 0) {
                if (iset2 == 0) {
                    max2 = hufmanTree[j].weight;
                    s2 = j;
                    iset2 = 1;
                    continue;
                }
                if (max2 > hufmanTree[j].weight) {
                    max2 = hufmanTree[j].weight;
                    s2 = j;
                }
            }
        }
        
        hufmanTree[s1].parent = i;
        hufmanTree[s2].parent = i;
        hufmanTree[i].leftChild = s1;
        hufmanTree[i].rightChild = s1;
        hufmanTree[i].weight = hufmanTree[s1].weight + hufmanTree[s2].weight;
    }
    
    int result =0;
    for(int i =1;i<=num;i++){
        
        if(hufmanTree[i].parent != 0){
            int temp= hufmanTree[i].parent;
            int count = 1;
            while(1){
                if(hufmanTree[temp].parent!=0){
                    temp = hufmanTree[temp].parent;
                    count++;
                    continue;
                }
                else break;
            }
            result += count * hufmanTree[i].weight;
        }
    }
    
    printf("WPL=%d\n",result);
    
    
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,924評論 18 139
  • 前面的文章主要從理論的角度介紹了自然語言人機(jī)對話系統(tǒng)所可能涉及到的多個(gè)領(lǐng)域的經(jīng)典模型和基礎(chǔ)知識。這篇文章,甚至之后...
    我偏笑_NSNirvana閱讀 14,072評論 2 64
  • 我是一個(gè)單身狗,可我現(xiàn)在卻要來跟大家討論“表白”——這個(gè)對我來說久違的事情。 (久違你也有臉給我們講!?。。?那又...
    看星星的人閱讀 727評論 1 1
  • 為了應(yīng)景,寫點(diǎn)和孩子有關(guān)的事情,但,也許無關(guān)。 1 昨天去了一所特殊學(xué)校,和孩子們待了一天。這里大多是語言遲緩和身...
    思潔大太陽哦閱讀 272評論 1 1
  • 總說生活源自根本,其實(shí)相處并不是一件易事。愛人、家人、朋友,以至身邊的各種關(guān)系,不與我相關(guān)的人和事,可以不去關(guān)心,...
    了了爍然閱讀 481評論 0 0