hdu(1053)

题目: http://acm.hdu.edu.cn/showproblem.php?pid=1053
霍夫曼编码的题目,要注意n==1时的特殊情况。

#include
#include
using namespace std;
const int inf = 1000000;
typedef struct hufcode{
    int weight;
    int parent;
    int depth;
    int lson;
    int rson;
    void set(){parent = -1;depth= 0;lson = -1;rson= -1;}
}hc;
hc *node;
int hash[30];
char str[100];

int build(int n){
    int maxn = 2*n - 1;
    int min1,min2,s1,s2;
    for(inti = 0;i  node[j].weight){
              s2 = s1;
              min2 = min1;
              min1 = node[j].weight;
              s1 = j;
           }
          else if(node[j].parent == -1 && min2> node[j].weight){
              min2 = node[j].weight;
              s2 = j;
           }
       }
       node[i+n].weight = node[s1].weight + node[s2].weight;
       node[i+n].parent = -1;
       node[i+n].lson = s1;
       node[i+n].rson = s2;
       node[s1].parent = n+i;
       node[s2].parent = n+i;
    }
    return maxn - 1;
}
void getdepth(int root,int depth){
    if(node[root].lson == -1 &&node[root].rson == -1)
       node[root].depth = depth;
    else if(node[root].lson == -1)
       getdepth(node[root].rson, depth+1);
    else if(node[root].rson == -1)
       getdepth(node[root].lson, depth+1);
    else{
       getdepth(node[root].lson, depth+1);
       getdepth(node[root].rson, depth+1);
    }
}
int main(){
    int root,result;
   node =(hufcode*)calloc(60,sizeof(hufcode));
    while(scanf("%s",str)&&strcmp(str,"END")){
       memset(hash, 0, sizeof(hash));
       int len =(int)strlen(str);
       for(inti = 0 ;i 


你可能感兴趣的:(数据结构)