Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

各位强人,帮忙看一下这个程序有啥问题啊?万分感谢,高山仰止!

Posted by Samwang at 2009-05-17 13:22:51 on Problem 1521 and last updated at 2009-05-17 13:28:41
#include<iostream>
#include<string>
using namespace std;

//const int 28=27+1;//1...26(字母),27(_)
const int inf=10000;

struct node//某个字母
{
	int time;//初始时他作为一个字母的出现次数
	int len;//Huffman构造过程中他为根的子树叶子路径权值和
	int w;  //Huffman构造过程中他为根的子树叶子    权值和
};

node arr[28];

//用一个最小堆
struct heap
{
	node *arr;
	int heapsize;

	heap(node *arr,int heapsize)
	{
		this->arr=arr;
		this->heapsize=heapsize;
	}
};

void heapify(heap &h,int pos)
{
	int i=pos;
	while(i<h.heapsize)
	{
		int sm=i;
		if(i*2<=h.heapsize && h.arr[i*2].w<h.arr[sm].w) sm=i*2;
	    if(i*2+1<=h.heapsize && h.arr[i*2+1].w<h.arr[sm].w) sm=i*2+1;
		if(sm!=i)
		{
			node temp=h.arr[i];
			h.arr[i]=h.arr[sm];
			h.arr[sm]=temp;
			i=sm;
		}
		else
			break;
	}
}

void build(heap &h)
{
	for(int i=h.heapsize/2;i>=1;i--)
	{
		heapify(h,i);
	}
}

node extract(heap &h)
{
	node temp=h.arr[1];
	h.arr[1]=h.arr[h.heapsize];
	h.arr[h.heapsize]=temp;
	h.heapsize--;
	heapify(h,1);//以w为比较依据

	return temp;
}

void decrease(heap &h,int pos,int w)
{
	//将h.arr[pos]的w域减少到int w,调整堆
    int cur=pos;
	int above=pos/2;

	h.arr[cur].w=w;
	while(above>=1)
	{
		if(h.arr[cur].w<h.arr[above].w)
		{
			node temp=h.arr[cur];
			h.arr[cur]=h.arr[above];
			h.arr[above]=temp;
			cur=above;
			above=cur/2;
		}
		else break;
	}
}

void insert(heap &h,node z)
{
	h.heapsize++;
	h.arr[h.heapsize]=z;
	int w=z.w;
	z.w=inf;
	decrease(h,h.heapsize,w);
}

void Huffman(int n,int asc_len)  //[1...n]
{
	heap h(arr,n);
	build(h);
	for(int i=1;i<=n-1;i++)
	{
		node x=extract(h);

		node y=extract(h);

		node z;

		z.len=x.len+y.len+x.w+y.w;
		z.w=x.w+y.w;
		insert(h,z);
	}
	node final=extract(h);
	cout<<final.len<<" ";

	cout.setf(cout.showpoint); 
    cout.precision(1); 
    cout.setf(ios::fixed); 
	float res=(float)asc_len/(float)final.len;
	cout<<res<<endl;
}

//已知字符串,求其哈弗曼编码的总长度
void fun(string str,int len,int asc_len)
{
	int pos;
	int dif[100];
	int i;
	for(i=1;i<=27;i++) 
	{
		dif[i]=0;//不同字母或'_'的出现次数统计(可能有间隔)
	}
	for(i=0;i<len;i++)
	{
		if(str.at(i)=='_')
			pos=27;
		else //为字母
			pos=str.at(i)%26+1;
		dif[pos]++;
	}

	int j=1;
	for(i=1;i<=27;i++)
	{
		if(dif[i]!=0)
		{
			arr[j].time=dif[i];
			arr[j].len=0;
			arr[j].w=arr[j].time;
		    j++;
		}
	}
	j--;

	Huffman(j,asc_len);
}

int main(void)
{
	string str;
	string end=string("END");
	cin>>str;
	while(str!=end)
	{
		int len=str.length();
		cout<<8*len<<" ";
		if(len==1) cout<<"1 "<<"8"<<endl;
		else fun(str,len,8*len);
		cin>>str;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator