zoukankan      html  css  js  c++  java
  • PKU 1521 Entropy(简单哈弗曼树_水过)

    题目大意:原题链接

    给你一个字符串,首先是计算出一个按正常编码的编码长度,其次是计算出一个用霍夫曼编码的编码长度,最后求正常编码的长度除以霍夫曼编码长度的比值,保留一位小数。

    解题思路:需要知道

    1.正常的编码长度的话,由于都是ASCII码值所以编码长度都为8,所以总长度就是8*字符串的长度Len就行。

    2.哈弗曼树带权路径之和WPL=非叶子节点权值之和

    刚开始一直不明白运行结果都正确了,为什么Compile Error,后来才知道原来iostream头文件不包含printf,所以又加上了cstdio头文件,也给了自己一个启发:

    以后头文件中可以两个都写,这样都能用cin,cout,scanf,printf都能用,很方便

    #include<queue>
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    string str;
    int ans,s[200];
    priority_queue<int,vector<int>,greater<int> > que;
    int main()
    {
        while(cin>>str){
            if(str=="END") return 0;
            memset(s,0,sizeof(s));
            sort(str.begin(),str.end());
            char ch=str[0];
            int len=str.length(),cnt=1;
            while(!que.empty()) que.pop();
            for(int i=1;i<len;i++){
                if(str[i]==ch) cnt++;
                else{
                    ch=str[i];
                    que.push(cnt);
                    cnt=1;
                }
            }
            que.push(cnt);
            if(que.size()==1) ans=que.top();
            else{
                ans=0;
                while(que.size()!=1){
                    int x=que.top();
                    que.pop();
                    int y=que.top();
                    que.pop();
                    ans+=(x+y);
                    que.push(x+y);
                }
            }
            double rate=(double)8*len/ans;
            printf("%d %d %.1f
    ",8*len,ans,rate);
        }
    }
  • 相关阅读:
    内存
    jmeter设置全局变量
    tomcat(1)
    JVM(一)
    内存溢出
    消息中间件
    上下文切换(二)
    平均负载(二)
    requests模块
    Pycharm如何配置Git
  • 原文地址:https://www.cnblogs.com/freinds/p/6421497.html
Copyright © 2011-2022 走看看