#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
typedef struct
{
int weight;
int parent,lchild,rchild;
}HTNode, *HuffmanTree;
typedef char * *HuffmanCode;
int min(HuffmanTree t,int i)
{
int j,flag=0;
int k=99999; // 取k为不小于可能的值
for(j=1; j<=i; j++)
if(t[j].weight<k&&t[j].parent==0)
k=t[j].weight,flag=j;
t[flag].parent=1;
return flag;
}
//本实习题中右子树是最小值对应序号,左子树是次小值对应序号
void select(HuffmanTree t,int i,int &s1,int &s2)
{
s2=min(t,i);
s1=min(t,i);
}
void Huffmancoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n)
{
int m,i,s1,s2,c,f,start;
char *cd;
if(n <= 1)
{
return;
}
m = 2*n-1;
HT = (HuffmanTree) malloc ((m+1)*sizeof(HTNode));
for(i = 1;i <= n;i++)
{
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
HT[i].weight = w[i-1];
}
for(;i <= m;i++)
{
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
HT[i].weight = 0;
}
for(i = n+1;i <= m;i++)
{
select(HT,i-1,s1,s2);
HT[s1].parent = i; HT[s2].parent = i;
HT[i].lchild = s1;HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
}
HC = (HuffmanCode)malloc((n+1)*sizeof(char *));
cd = (char *)malloc(n*sizeof(char));
cd[n-1] = '\0';
for(i = 1;i <= n;i++)
{
start = n-1;
for(c = i,f = HT[i].parent;f != 0;c = f,f = HT[f].parent)
{
if(HT[f].lchild == c)
{
cd[--start] = '1';
}
else
{
cd[--start] = '0';
}
}
HC[i] = (char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
free(cd);
}
int main()
{
int i,n,t,j;
int w[26];
char a[100];
char s[26];
HuffmanTree HT;
HuffmanCode HC;
cin >> n;
for(i = 0;i < n;i++)
{cin >> s[i];}
for(i = 0;i < n;i++)
{cin >> w[i];}
cin.get();
Huffmancoding(HT,HC,w,n);
cin.getline(a,100);
int len = strlen(a);
for(j = 0;j < len;j++)
{
t = a[j]-'a'+1;
cout << HC[t];
}
cout << endl;
cout << a << endl;
return 0;
}
问题:
语言: C C++ GCC G++ Java
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
typedef struct
{
int weight;
int parent,lchild,rchild;
}HTNode, *HuffmanTree;
typedef char * *HuffmanCode;
int min(HuffmanTree t,int i)
{
int j,flag=0;
int k=99999; // 取k为不小于可能的值
for(j=1; j<=i; j++)
if(t[j].weight<k&&t[j].parent==0)
k=t[j].weight,flag=j;
t[flag].parent=1;
return flag;
}
//本实习题中右子树是最小值对应序号,左子树是次小值对应序号
void select(HuffmanTree t,int i,int &s1,int &s2)
{
s2=min(t,i);
s1=min(t,i);
}
void Huffmancoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n)
{
int m,i,s1,s2,c,f,start;
char *cd;
if(n <= 1)
{
return;
}
m = 2*n-1;
HT = (HuffmanTree) malloc ((m+1)*sizeof(HTNode));
for(i = 1;i <= n;i++)
{
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
HT[i].weight = w[i-1];
}
for(;i <= m;i++)
{
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
HT[i].weight = 0;
}
for(i = n+1;i <= m;i++)
{
select(HT,i-1,s1,s2);
HT[s1].parent = i; HT[s2].parent = i;
HT[i].lchild = s1;HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
}
HC = (HuffmanCode)malloc((n+1)*sizeof(char *));
cd = (char *)malloc(n*sizeof(char));
cd[n-1] = '\0';
for(i = 1;i <= n;i++)
{
start = n-1;
for(c = i,f = HT[i].parent;f != 0;c = f,f = HT[f].parent)
{
if(HT[f].lchild == c)
{
cd[--start] = '1';
}
else
{
cd[--start] = '0';
}
}
HC[i] = (char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
free(cd);
}
int main()
{
int i,n,t,j;
int w[26];
char a[100];
char s[26];
HuffmanTree HT;
HuffmanCode HC;
cin >> n;
for(i = 0;i < n;i++)
{cin >> s[i];}
for(i = 0;i < n;i++)
{cin >> w[i];}
cin.get();
Huffmancoding(HT,HC,w,n);
cin.getline(a,100);
int len = strlen(a);
for(j = 0;j < len;j++)
{
t = a[j]-'a'+1;
cout << HC[t];
}
cout << endl;
cout << a << endl;
return 0;
}
问题:
语言: C C++ GCC G++ Java
#include <stdlib.h>
#include <cstring>
using namespace std;
typedef struct
{
int weight;
int parent,lchild,rchild;
}HTNode, *HuffmanTree;
typedef char * *HuffmanCode;
int min(HuffmanTree t,int i)
{
int j,flag=0;
int k=99999; // 取k为不小于可能的值
for(j=1; j<=i; j++)
if(t[j].weight<k&&t[j].parent==0)
k=t[j].weight,flag=j;
t[flag].parent=1;
return flag;
}
//本实习题中右子树是最小值对应序号,左子树是次小值对应序号
void select(HuffmanTree t,int i,int &s1,int &s2)
{
s2=min(t,i);
s1=min(t,i);
}
void Huffmancoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n)
{
int m,i,s1,s2,c,f,start;
char *cd;
if(n <= 1)
{
return;
}
m = 2*n-1;
HT = (HuffmanTree) malloc ((m+1)*sizeof(HTNode));
for(i = 1;i <= n;i++)
{
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
HT[i].weight = w[i-1];
}
for(;i <= m;i++)
{
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
HT[i].weight = 0;
}
for(i = n+1;i <= m;i++)
{
select(HT,i-1,s1,s2);
HT[s1].parent = i; HT[s2].parent = i;
HT[i].lchild = s1;HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
}
HC = (HuffmanCode)malloc((n+1)*sizeof(char *));
cd = (char *)malloc(n*sizeof(char));
cd[n-1] = '\0';
for(i = 1;i <= n;i++)
{
start = n-1;
for(c = i,f = HT[i].parent;f != 0;c = f,f = HT[f].parent)
{
if(HT[f].lchild == c)
{
cd[--start] = '1';
}
else
{
cd[--start] = '0';
}
}
HC[i] = (char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
free(cd);
}
int main()
{
int i,n,t,j;
int w[26];
char a[100];
char s[26];
HuffmanTree HT;
HuffmanCode HC;
cin >> n;
for(i = 0;i < n;i++)
{cin >> s[i];}
for(i = 0;i < n;i++)
{cin >> w[i];}
cin.get();
Huffmancoding(HT,HC,w,n);
cin.getline(a,100);
int len = strlen(a);
for(j = 0;j < len;j++)
{
t = a[j]-'a'+1;
cout << HC[t];
}
cout << endl;
cout << a << endl;
return 0;
}
问题:
语言: C C++ GCC G++ Java
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
typedef struct
{
int weight;
int parent,lchild,rchild;
}HTNode, *HuffmanTree;
typedef char * *HuffmanCode;
int min(HuffmanTree t,int i)
{
int j,flag=0;
int k=99999; // 取k为不小于可能的值
for(j=1; j<=i; j++)
if(t[j].weight<k&&t[j].parent==0)
k=t[j].weight,flag=j;
t[flag].parent=1;
return flag;
}
//本实习题中右子树是最小值对应序号,左子树是次小值对应序号
void select(HuffmanTree t,int i,int &s1,int &s2)
{
s2=min(t,i);
s1=min(t,i);
}
void Huffmancoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n)
{
int m,i,s1,s2,c,f,start;
char *cd;
if(n <= 1)
{
return;
}
m = 2*n-1;
HT = (HuffmanTree) malloc ((m+1)*sizeof(HTNode));
for(i = 1;i <= n;i++)
{
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
HT[i].weight = w[i-1];
}
for(;i <= m;i++)
{
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
HT[i].weight = 0;
}
for(i = n+1;i <= m;i++)
{
select(HT,i-1,s1,s2);
HT[s1].parent = i; HT[s2].parent = i;
HT[i].lchild = s1;HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
}
HC = (HuffmanCode)malloc((n+1)*sizeof(char *));
cd = (char *)malloc(n*sizeof(char));
cd[n-1] = '\0';
for(i = 1;i <= n;i++)
{
start = n-1;
for(c = i,f = HT[i].parent;f != 0;c = f,f = HT[f].parent)
{
if(HT[f].lchild == c)
{
cd[--start] = '1';
}
else
{
cd[--start] = '0';
}
}
HC[i] = (char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
free(cd);
}
int main()
{
int i,n,t,j;
int w[26];
char a[100];
char s[26];
HuffmanTree HT;
HuffmanCode HC;
cin >> n;
for(i = 0;i < n;i++)
{cin >> s[i];}
for(i = 0;i < n;i++)
{cin >> w[i];}
cin.get();
Huffmancoding(HT,HC,w,n);
cin.getline(a,100);
int len = strlen(a);
for(j = 0;j < len;j++)
{
t = a[j]-'a'+1;
cout << HC[t];
}
cout << endl;
cout << a << endl;
return 0;
}
问题:
语言: C C++ GCC G++ Java