/ 科创班 /

记录详情

Wrong Answer

/in/foo.cc: In function 'void HuffmanCoding(HTNode*, HTCode*, int)':
/in/foo.cc:86:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if(ht[f].lchild == c)
                ~~~~~~~~~~~~~^~~~
/in/foo.cc:61:11: warning: unused variable 'j' [-Wunused-variable]
     int i,j,m,c,f,s1,s2,start;
           ^
/in/foo.cc: In function 'int main()':
/in/foo.cc:98:17: warning: unused variable 'm' [-Wunused-variable]
     int i,sum=0,m,n,w[N+1];
                 ^
/in/foo.cc:98:21: warning: unused variable 'w' [-Wunused-variable]
     int i,sum=0,m,n,w[N+1];
                     ^
# 状态 耗时 内存占用
#1 Accepted 2ms 384.0 KiB
#2 Accepted 2ms 364.0 KiB
#3 Accepted 1ms 372.0 KiB
#4 Accepted 2ms 256.0 KiB
#5 Accepted 2ms 372.0 KiB
#6 Wrong Answer 1ms 380.0 KiB
#7 Wrong Answer 2ms 364.0 KiB
#8 Accepted 1ms 352.0 KiB
#9 Wrong Answer 2ms 376.0 KiB
#10 Accepted 3ms 344.0 KiB
#11 Accepted 3ms 384.0 KiB
#12 Accepted 3ms 360.0 KiB
#13 Wrong Answer 1ms 384.0 KiB
#14 Wrong Answer 2ms 368.0 KiB
#15 Accepted 2ms 372.0 KiB
#16 Accepted 1ms 380.0 KiB
#17 Wrong Answer 2ms 356.0 KiB
#18 Accepted 3ms 344.0 KiB
#19 Wrong Answer 3ms 256.0 KiB
#20 Accepted 1ms 384.0 KiB

代码

#include<iostream>  
#include<cstdio>  
#include<cstring>  
using namespace std;  
#define N 100
#define M (2*N-1)
class HTNode
{
	public:   
    	unsigned int weight;  
    	unsigned int parent,lchild,rchild;  
};                      
  
class HTCode
{  
	public:  
    	int weight;
    	char code[N];
};  
  
void Init(HTCode hc[], int *n)
{
    int i;   
    cin>>*n;
    
    fflush(stdin);  
    for(i=1; i<=*n; ++i)  
        cin>>hc[i].weight;  
    fflush(stdin);  
}//  
  
void Select(HTNode ht[], int k, int *s1, int *s2)
{
    int i;  
    for(i=1; i<=k && ht[i].parent != 0; ++i)
    {
    	;
    }
    *s1 = i;  
  
    for(i=1; i<=k; ++i){  
        if(ht[i].parent==0 && ht[i].weight<ht[*s1].weight)  
            *s1 = i;  
    }  
  
    for(i=1; i<=k; ++i){  
        if(ht[i].parent==0 && i!=*s1)  
            break;  
    }  
    *s2 = i;  
  
    for(i=1; i<=k; ++i){  
        if(ht[i].parent==0 && i!=*s1 && ht[i].weight<ht[*s2].weight)  
            *s2 = i;  
    }  
}  
  
void HuffmanCoding(HTNode ht[],HTCode hc[],int n)
{
    char cd[N];  
    int i,j,m,c,f,s1,s2,start;  
    m = 2*n-1;  
      
    for(i=1; i<=m; ++i){  
        if(i <= n)  
            ht[i].weight = hc[i].weight;  
        else  
            ht[i].parent = 0;  
        ht[i].parent = ht[i].lchild = ht[i].rchild = 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;  
    }  
  
    cd[n-1] = '\0';  
  
    for(i=1; i<=n; ++i){  
        start = n-1;  
        for(c=i,f=ht[i].parent; f; c=f,f=ht[f].parent){  
            if(ht[f].lchild == c)  
                cd[--start] = '0';  
            else  
                cd[--start] = '1';  
        }  
        strcpy(hc[i].code, &cd[start]);  
    }  
}  
  
  
int main()  
{  
    int i,sum=0,m,n,w[N+1];  
    HTNode ht[M+1];  
    HTCode hc[N+1];  
    Init(hc, &n);
    HuffmanCoding(ht,hc,n);

    for(i=1; i<=n; ++i) 
    {
    	sum=sum+strlen(hc[i].code)*hc[i].weight;
    }
    cout<<sum;
    return 0;  
}  

信息

递交者
类型
递交
题目
哈夫曼树
题目数据
下载
语言
C++
递交时间
2018-05-15 19:42:48
评测时间
2018-05-15 19:42:48
评测机
分数
65
总耗时
52ms
峰值内存
384.0 KiB