设为首页 加入收藏

TOP

HDU-1053-Entropy(Huffman编码)(二)
2015-07-20 17:34:02 来源: 作者: 【 】 浏览:7
Tags:HDU-1053-Entropy Huffman 编码
ding. The letters “C”, “I’ and “N” only occur once, however, so they will have the longest codes.

There are many possible sets of prefix-free variable-length bit patterns that would yield the optimal encoding, that is, that would allow the text to be encoded in the fewest number of bits. One such optimal encoding is to encode spaces with “00”, “A” with “100”, “C” with “1110”, “E” with “1111”, “H” with “110”, “I” with “1010”, “N” with “1011” and “T” with “01”. The optimal encoding therefore requires only 51 bits compared to the 144 that would be necessary to encode the message with 8-bit ASCII encoding, a compression ratio of 2.8 to 1.

Input The input file will contain a list of text strings, one per line. The text strings will consist only of uppercase alphanumeric characters and underscores (which are used in place of spaces). The end of the input will be signalled by a line containing only the word “END” as the text string. This line should not be processed.

Output For each text string in the input, output the length in bits of the 8-bit ASCII encoding, the length in bits of an optimal prefix-free variable-length encoding, and the compression ratio accurate to one decimal point.

Sample Input
AAAAABCD
THE_CAT_IN_THE_HAT
END

Sample Output
64 13 4.9
144 51 2.8

Source Greater New York 2000
思路:此题不需要编码,只需要求编码的长度而已,直接乱搞。
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       using namespace std; int num[27]; char s[100005]; struct S{ int val; S(int a){val=a;} bool operator<(const S &p) const { return val>p.val; } }; int main() { int n,i,len,ans,a,b,cnt; while(~scanf("%s",s)) { len=strlen(s); if(len==3 && s[0]=='E' && s[1]=='N' && s[2]=='D') return 0; memset(num,0,sizeof num); for(i=0;i
      
       que; cnt=0; for(i=0;i<27;i++) if(num[i]) { que.push(num[i]); cnt++; } if(cnt==1)//特判 { printf("%d %d %.1f\n",len*8,len,8.0); continue; } ans=0; while(que.size()>1) { a=que.top().val; que.pop(); b=que.top().val; que.pop(); ans+=a+b; que.push(a+b); } printf("%d %d %.1f\n",len*8,ans,(double)len*8/ans); } }
      
     
    
   
  
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 1979 Red and Black (深度优.. 下一篇hdu 1185 状压dp 好题 (当前状态..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·每日一道面试题-多线 (2025-12-26 06:20:17)
·java项目中哪些地方 (2025-12-26 06:20:14)
·Java真的是要没落了 (2025-12-26 06:20:12)
·C++ Lambda表达式保 (2025-12-26 05:49:45)
·C++ Lambda表达式的 (2025-12-26 05:49:42)