可以与Hdu 2527 用一样水过,因为这里不需要求编码,但我写这一题主要是为了学习哈弗曼建树,所以也用建树的写了一遍。
CODE:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;
#define MAXN 1001
priority_queue<int , vector<int>, greater<int> > q;
char str[MAXN];
int huf[MAXN];
int len;
void solve()
{
int ans = 0, a, b;
while(q.size() != 1)
{
a = q.top(); q.pop();
b = q.top(); q.pop();
q.push(a+b);
ans += (a+b);
}
printf("%d %d %.1lf\n", len*8, ans, len*8.0/ans);
}
void init()
{
while(!q.empty()) q.pop();
memset(huf, 0, sizeof(huf));
}
int main()
{
while(scanf("%s", str) && strcmp(str, "END"))
{
init();
len = strlen(str);
for(int i = 0; i < len; i++)
{
huf[str[i]]++;
}
int ans1 = len*8;
for(int i = 0; i < 100; i++) if(huf[i])
{
q.push(huf[i]);
}
if(q.size() == 1)
{
printf("%d %d 8.0\n", ans1, len);
continue;
}
solve();
}
return 0;
}
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;
#define MAXN 1001
priority_queue<int , vector<int>, greater<int> > q;
char str[MAXN];
int huf[MAXN];
int len;
void solve()
{
int ans = 0, a, b;
while(q.size() != 1)
{
a = q.top(); q.pop();
b = q.top(); q.pop();
q.push(a+b);
ans += (a+b);
}
printf("%d %d %.1lf\n", len*8, ans, len*8.0/ans);
}
void init()
{
while(!q.empty()) q.pop();
memset(huf, 0, sizeof(huf));
}
int main()
{
while(scanf("%s", str) && strcmp(str, "END"))
{
init();
len = strlen(str);
for(int i = 0; i < len; i++)
{
huf[str[i]]++;
}
int ans1 = len*8;
for(int i = 0; i < 100; i++) if(huf[i])
{
q.push(huf[i]);
}
if(q.size() == 1)
{
printf("%d %d 8.0\n", ans1, len);
continue;
}
solve();
}
return 0;
}
CODE:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;
const int MAXN = 1010;
struct node
{
int data;
int dep;
node *L, *R;
node()
{
data = 0;
dep = 0;
L = NULL;
R = NULL;
}
bool operator <(node a) const
{
return a.data < data;
} //重载小于号
};
int ans;
char str[MAXN];
int huf[MAXN];
priority_queue<node> Q;
void BuildHuffman()
{
node *p, *q, *T;
while(Q.size() != 1)
{
p = new node();
q = new node();
T = new node();
*p = Q.top(); Q.pop();
*q = Q.top(); Q.pop();
T->data = p->data + q->data;
T->L = p, T->R = q;
Q.push(*T);
}
return ;
}
queue<node> q;
void solve()
{
node root;
root = Q.top(); Q.pop();
root.dep = 0;
q.push(root);
while(!q.empty())
{
node x = q.front(); q.pop();
if(x.L)
{
x.L->dep = x.dep + 1;
q.push(*(x.L));
}
if(x.R)
{
x.R->dep = x.dep + 1;
q.push(*(x.R));
}
if(!x.L && !x.R)
{
ans += x.dep * x.data;
}
}
}
/*void dfs(node *p, int dep)
{
if(!p->L && !p->R)
{
ans += p->data * dep;
}
if(p->L)
{
dfs(p->L, dep+1);
}
if(p->R)
{
dfs(p->R, dep+1);
}
}*/
void init()
{
ans = 0;
while(!Q.empty()) Q.pop();
while(!q.empty()) q.pop();
memset(huf, 0, sizeof(huf));
}
int main()
{
while(scanf("%s", str) && strcmp(str, "END"))
{
init();
int len = strlen(str);
for(int i = 0; str[i]; i++)
{
huf[str[i]]++;
}
for(int i = 0; i < 100; i++) if(huf[i])
{
node t;
t.data = huf[i];
Q.push(t);
}
if(Q.size() == 1) //特判
{
printf("%d %d 8.0\n", len*8, len);
continue;
}
BuildHuffman();
solve();
//dfs(root, 0); 用DFS也可以实现
printf("%d %d %.1lf\n", len*8, ans, len*8.0/ans);
}
return 0;
}
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;
const int MAXN = 1010;
struct node
{
int data;
int dep;
node *L, *R;
node()
{
data = 0;
dep = 0;
L = NULL;
R = NULL;
}
bool operator <(node a) const
{
return a.data < data;
} //重载小于号
};
int ans;
char str[MAXN];
int huf[MAXN];
priority_queue<node> Q;
void BuildHuffman()
{
node *p, *q, *T;
while(Q.size() != 1)
{
p = new node();
q = new node();
T = new node();
*p = Q.top(); Q.pop();
*q = Q.top(); Q.pop();
T->data = p->data + q->data;
T->L = p, T->R = q;
Q.push(*T);
}
return ;
}
queue<node> q;
void solve()
{
node root;
root = Q.top(); Q.pop();
root.dep = 0;
q.push(root);
while(!q.empty())
{
node x = q.front(); q.pop();
if(x.L)
{
x.L->dep = x.dep + 1;
q.push(*(x.L));
}
if(x.R)
{
x.R->dep = x.dep + 1;
q.push(*(x.R));
}
if(!x.L && !x.R)
{
ans += x.dep * x.data;
}
}
}
/*void dfs(node *p, int dep)
{
if(!p->L && !p->R)
{
ans += p->data * dep;
}
if(p->L)
{
dfs(p->L, dep+1);
}
if(p->R)
{
dfs(p->R, dep+1);
}
}*/
void init()
{
ans = 0;
while(!Q.empty()) Q.pop();
while(!q.empty()) q.pop();
memset(huf, 0, sizeof(huf));
}
int main()
{
while(scanf("%s", str) && strcmp(str, "END"))
{
init();
int len = strlen(str);
for(int i = 0; str[i]; i++)
{
huf[str[i]]++;
}
for(int i = 0; i < 100; i++) if(huf[i])
{
node t;
t.data = huf[i];
Q.push(t);
}
if(Q.size() == 1) //特判
{
printf("%d %d 8.0\n", len*8, len);
continue;
}
BuildHuffman();
solve();
//dfs(root, 0); 用DFS也可以实现
printf("%d %d %.1lf\n", len*8, ans, len*8.0/ans);
}
return 0;
}