TOJ 1225 data structure exercises--huffman Coding

Source: Internet
Author: User
Tags time limit
data structure exercises--huffman Coding time limit (normal/java): 1000ms/10000ms Run memory limit: 65536KByte

Description

Previously, when there was no phone, the main means of fast long-distance communication was the Telegraph, which converted the text needed to be transferred into a string of binary characters. For example, suppose that the message to be transmitted is ' a B a C c D a ', which has only four characters, giving each character a weight (that is, the number of characters appearing in the message) according to the probability of the occurrence of the character, for example:
A:3
B:1
C:2
D:1

At this point we have a code called the Huffman code can compress the above message (without this technology, people can be how much you money.) ), now you need to complete the Huffman code to find the last average code length.

input

Input data has more than one group, the first line input test data Group M. In the next m line, each line begins with an n value, representing how many different characters are in the message, followed by n positive integers, representing the weights of each character. Suppose the number of characters is not more than 100 (English is not enough to use Chinese well ^_^).

Output

Output the average code length after Huffman encoding for each group of messages, leaving 3 digits after the decimal point.

Sample Input

2
2 1 1
4 3 1 2 1

Sample Output

1.000
2.250

#include <stdio.h>
#include <queue>
using namespace std;

struct node
{
	int x, y;
	friend bool Operator < (node A,node b)
	{	
		if (a.x!=b.x)
			return a.x>b.x;
		Return a.y>b.y;
	}
};
Main ()
{
	node x, y;
	int t,n,sum,k,m;
	scanf ("%d", &t);
	while (t--)
	{
		sum = 0;
		Priority_queue <node > Q;
		scanf ("%d", &n);
		m = n;
		while (m--)
		{
			scanf ("%d", &x.x);
			x.y = 1;
			Q.push (x);
		}
		while (Q.size ()!=1)
		{
			x = Q.top ();
			Q.pop ();
			y = Q.top ();
			Q.pop ();
			K = x.y+y.y;
			x.x =  x.x+y.x;
			x.y = k;
			sum + = k;
			Q.push (x);
		}
		printf ("%.3lf\n", double (sum)/double (n));
	}
}


#include <stdio.h> #define INF 0x7ffff; struct node {int w,parent,lchild,rchild;}
TREE[202];
	struct code {int start;
int d[10];
}HUFF[202];
int n;
	void creat () {int i,j,m,w1,w2,w,x,y;
	m=2*n-1;
	for (i=1;i<=m;i++) {tree[i].parent=tree[i].lchild=tree[i].rchild=0;
		} for (i=n+1;i<=m;i++) {w1=w2=inf;
		x=y=0;
					for (j=1;j<i;j++) {if (!tree[j].parent) {if (W1&GT;TREE[J].W) {w2=w1;
					Y=x;
					W1=TREE[J].W;
				X=j;
					} else if (W2&GT;TREE[J].W) {y=j;
				W2=TREE[J].W;
		}}} tree[i].w=w1+w2;
		tree[x].parent=tree[y].parent=i;
		Tree[i].lchild=x;
	Tree[i].rchild=y;
	}} void Creat_huff () {int i,c,f;
		for (i=1;i<=n;i++) {huff[i].start=n;
			for (c=i,f=tree[i].parent;f;c=f,f=tree[f].parent) {if (c==tree[f].lchild) huff[i].d[huff[i].start--]=0;
		else huff[i].d[huff[i].start--]=1;
	}}} main () {int t,x,i;
	scanf ("%d", &t);
		while (t--) {scanf ("%d", &n);
		for (i=1;i<=n;i++)	scanf ("%d", &AMP;TREE[I].W);
		Creat ();
		Creat_huff ();
		Double sum=0.0;
		for (i=1;i<=n;i++) Sum+=n-huff[i].start;
		Sum/=n;
	printf ("%.3f\n", sum);
 }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.