HDU 2527 Safe Or Unsafe

http://acm.hdu.edu.cn/showproblem.php?pid=2527


题目很明显的说明了用到哈夫曼树的知识,要将哈夫曼编码值与安全值进行比较,那么什么是哈夫曼编码值呢?

这题所要求的就是将非叶子节点的权值进行相加,然后与安全值比较。

提供两个种代码。


代码一:用队列做,因为每次要将最小的与次小的两个数重新组成一个数,再将这个数入队。

#include<iostream>
#include<string>
#include<queue>
using namespace std;

struct node
{
	friend bool operator < (node a,node b)
	{
		return a.t > b.t;
	}
	int t;
};

void main()
{
	int t;
	int i,j,m,n,a,b;
	char str[100];
	int num2[26];

	while (cin>>t)
	{
		while(t--)
		{
	          cin>>n;
			  cin>>str;
			  memset(num2,0,sizeof(num2));
			  queue <node> q;
			  int len =  strlen(str);
			  for(i=0;i<len;i++)
			  {
                  num2[str[i] - 'a']++;
			  }

			  node t;
			  for(i = 0,j=0;i<26;i++)
			  {
				  if(num2[i] != 0)
				  {
					  t.t = num2[i];
					  q.push(t);
					  j++;
				  }
					  
			  }
              
			  if(j == 1)
			  {
				  if(len <= n)
					  cout<<"yes";
				  else
					  cout<<"no";
				  cout<<endl;
			  }
			  else
			  {
				  i  =0;
				  
				  int sum = 0,temp = 0;
				  while(i < j -1)
				  {
					  temp = q.front().t;
					  q.pop();
					  temp += q.front().t;
					  q.pop();
					  t.t = temp;
					  sum += temp;
					  q.push(t);
					  i++;
				  }
				  
				  if(sum > n)
					  cout<<"no";
				  else
					  cout<<"yes";
			   cout<<endl;
			  }
			 
		}
	}
}



代码二:就是对前面的哈夫曼的文章中的代码进行改造,去掉编码那块的代码就可以了。


#include <iostream>  
#include <stdlib.h>  
#include <string>
#define MaxValue 1000  
using namespace std;  
  
struct tree  
{  
    int parent;  
    int lchild;  
    int rchild;  
    int weight;  
    int flag;  
}myhufftree[100];  
  
// struct code  
// {  
//     int bit[10]; //倒序存  
//     int ary[10];  //顺序存  
//     int len;     //每个编码的长度  
// }myhuffcode[10];  
  
int summ;

void hufftree(int n ,int weight[])  
{  
    //包括初始化和构建树  
    int x1,x2,m1,m2;  
    int i ,j;  
  
    for(i = 0;i < 2*n - 1;i++)  //初始化  
    {  
        if(i < n)  
            myhufftree[i].weight = weight[i];  
        else  
            myhufftree[i].weight = 0;  
  
        myhufftree[i].parent = 0;  
        myhufftree[i].flag = 0;  
        myhufftree[i].lchild = myhufftree[i].rchild = -1;  
    }  
      
    for(i = 0; i < n - 1;i++)  //构造哈弗曼树  这里的两个循环要好好看  
    {  
        m1 = m2 = MaxValue;  
        x1 = x2 = 0;  
        for(j = i;j < n + i;j++)  
        {  
            if(myhufftree[j].weight < m1 && myhufftree[j].flag == 0)  
            {  
                m2 = m1;  
                m1 = myhufftree[j].weight;            
                x2 = x1;  
                x1 = j;               
            }  
            else  
            {  
                if(myhufftree[j].weight < m2 && myhufftree[j].flag == 0)  
                {  
                    m2 = myhufftree[j].weight;  
                    x2 = j;  
                }  
            }  
        }  
        myhufftree[x1].parent = n + i;  
        myhufftree[x2].parent = n + i;  
        myhufftree[n+i].lchild = x1;  
        myhufftree[n+i].rchild = x2;  
        myhufftree[x1].flag = myhufftree[x2].flag = 1;  
        myhufftree[n+i].flag = 0;  
        myhufftree[n+i].weight = myhufftree[x1].weight + myhufftree[x2].weight;  
		summ += myhufftree[n+i].weight;

    }  
  
}  
  
// void huffcode(int n)  
// {//进行哈夫曼编码  
//     int i, j ;  
//     int child;  
//     int parent;  
//     int len;  
//   
//     for(i = 0;i < n;i++)  
//     {  
//         child = i;  
//         parent = myhufftree[i].parent;  
//         len = 0;  
// 		
//         while(parent != 0)    //对每次最小的两个下标进行组合  
//         {  
//             if(myhufftree[parent].lchild == child)  
//                 myhuffcode[i].bit[len] = 0;  
//             else  
//                 myhuffcode[i].bit[len] = 1;  
// 			
//             len++;  
//             child = parent;  
//             parent = myhufftree[parent].parent;  
// 			
//         }  
// 		
//         myhuffcode[i].len = len;  
// 		
//         for(j = 0;j < len;j++)   //将编码顺序存入数组  
//         {  
//             myhuffcode[i].ary[j] = myhuffcode[i].bit[len - j - 1];  
//         }  
//     }  
// }  

void main()  
{    
    int weight[26];  
    int n;  
    int sum;  
    int i ,j;  
	int t,m;
    int safe;
    char str[50];
	int num[26],num2[26];

    cin>>t;
	while(t--)
	{
        cin>>safe;/*scanf("%d",&safe)*///cin>>safe;
		cin>>str;

		int len = strlen(str);
		summ = 0;
		memset(num2,0,sizeof(num2));
		memset(weight,0,sizeof(weight));
        
		for(i = 0;i< len;i++)
		{
             num2[str[i] - 'a']++;
		}

		for(i=0,j=0;i<26;i++)
		{
			if(num2[i] != 0)
				weight[j++] = num2[i];
		}

		if(j == 1)
		{
			if(len > safe)
				cout<<"no";
			else
				cout<<"yes";

		}
		else
		{
              hufftree(j,weight);  
			  if(summ > safe)
				  cout<<"no";
			  else
			     cout<<"yes";
		}

		cout<<endl;
	}
    
	 
};  
怎么发现我编译器有问题,,提交时AC的,竟然编译错误。。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值