哈夫曼树 HDU 1053 HDU 2527

#include / * HDU 2527 裸的哈夫曼树 * /
#include
#include
using namespace std;
#define manx 100
#define inf 9999999;

mapmp;

struct node{
    int weight;
    int parent;
    int lch;
    int rch;
    string code;  ///  哈夫曼码值 
    string val;  ///  字符 
};

void init(node *hfnode,int n){ /// 初始化 
    for(int i=0;i<(n<<1)-1;i++){
        hfnode[i].weight=0;
        hfnode[i].parent=-1;
        hfnode[i].lch = hfnode[i].rch = -1;
        hfnode[i].code.clear();
        hfnode[i].val.clear();
    }
}

void make_tree(node *hfnode,int n){  /// 建哈夫曼树 
    int m1, m2, x1, x2;
    for(int i=1;i 0) {
        hfnode[ hfnode[m].lch ].code = "0";
    }
    if(hfnode[m].rch > 0) {
        hfnode[ hfnode[m].rch ].code = "1";
    }
    tree_code( hfnode, hfnode[m].lch, n ); 
    tree_code( hfnode, hfnode[m].rch, n ); 
}

string e,hh;  /// hh 表示哈夫曼编码,它的长度也表示深度 
              /// e 表示目标 值 

void tree_dfs( node *hfnode,int m,int n,string s,int &flag ){  /// 深搜 
    if( hfnode[m].val == e ){  /// 查询 
        flag=1, hh=s;  
        return ;
    }
    if(flag || m <= n) return ;
    if(hfnode[m].lch > 0) {
        tree_dfs(hfnode,hfnode[m].lch, n, s+hfnode[ hfnode[m].lch ].code, flag);
    }
    if(hfnode[m].rch > 0) {
        tree_dfs(hfnode,hfnode[m].rch, n, s+hfnode[ hfnode[m].rch ].code, flag);
    }
}

int main(){
    node hfnode[manx*4];
    string str;
    int n1,t;
    cin>>t;
    while(t--){ 
        int ans1;
        cin>>ans1>>str;
        mp.clear();
        n1 = str.size();
        init(hfnode,manx);
        int n=0,ans=1;
        for(int i=0;i


其实挺简单的,前几天自己写的课设就是哈夫曼树..

题意:要你求 8*str.size()   , sum(最优权值),   8*str.size()  /  (sum*1.0) 

代码:

/*

因为总计就是27个字符(状态),所以深搜是完全没有压力的 

*/ 

#include
#include
#include
using namespace std;
#define manx 100
#define inf 9999999;

mapmp;

struct node{
    int weight;
    int parent;
    int lch;
    int rch;
    string code;  ///  哈夫曼码值 
    string val;  ///  字符 
};

void init(node *hfnode,int n){ /// 初始化 
    for(int i=0;i<(n<<1)-1;i++){
        hfnode[i].weight=0;
        hfnode[i].parent=-1;
        hfnode[i].lch = hfnode[i].rch = -1;
        hfnode[i].code.clear();
        hfnode[i].val.clear();
    }
}

void make_tree(node *hfnode,int n){  /// 建哈夫曼树 
    int m1, m2, x1, x2;
    for(int i=1;i 0) {
        hfnode[ hfnode[m].lch ].code = "0";
    }
    if(hfnode[m].rch > 0) {
        hfnode[ hfnode[m].rch ].code = "1";
    }
    tree_code( hfnode, hfnode[m].lch, n );  
    tree_code( hfnode, hfnode[m].rch, n ); 
}

string e,hh; ///  hh表示哈夫曼编码,它的长度也算是深度
             ///  e 表示目标值 

void tree_dfs( node *hfnode,int m,int n,string s,int &flag ){  /// 深搜 
    if( hfnode[m].val == e ){  /// 当查询到时 
        flag=1, hh=s;
        return ;
    }
    if(flag || m <= n) return ; ///  两个成立的条件 
    if(hfnode[m].lch > 0) {
        tree_dfs(hfnode,hfnode[m].lch, n, s+hfnode[ hfnode[m].lch ].code, flag);
    }
    if(hfnode[m].rch > 0) {
        tree_dfs(hfnode,hfnode[m].rch, n, s+hfnode[ hfnode[m].rch ].code, flag);
    }
}

int main(){
    node hfnode[manx*4];
    string str;
    int n1; 
    while(getline(cin,str)){
        mp.clear();
        if(str=="END") break;
        n1 = str.size();
        init(hfnode,manx);
        int n=0,ans=1;
        for(int i=0;i


 

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