POJ 1521

简介: //Huffman树的构造就不赘述了,使用优先队列每次选择队头的两个数并将其出列,相加后将结果放入队列中,直到队列为空为止. #include #include #include #include #include #include using namespace st...
//Huffman树的构造就不赘述了,使用优先队列每次选择队头的两个数并将其出列,相加后将结果放入队列中,直到队列为空为止.
#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;
int main()
{
    int i,j,k;
    string s;
    while(1)//cin遇到空格结束 
    {
        getline(cin,s);
        if(s=="END")
            break;
        priority_queue <int , vector <int>,greater<int> > q;
        sort(s.begin(),s.end());
        /*
        为什么要排序?
        因为下面用到的字符分类统计是和下一个比较,
        不相同就立马入队,若是相同的字符中间还有其他字符
        会被认作是不同字符,因为q.size()表示的是字符种类
        */ 
        int cnt=0;
        char ch = s[0];
        for(i=0;i<s.length();i++)//字符分类统计 
        if(s[i]==ch)
            cnt++;
        else
        {
            q.push(cnt);
            cnt=1;//因为执行到头了,i该自增了 
            ch=s[i];
        }
        q.push(cnt);//这句原来忘了
        int old_len = s.length()*8;
        int new_len = 0;
        //if(s.length()==1)
           // new_len = 1;
        if(q.size()==1)//上面的语句输入AAA时,只有一种字符,此时new_len=0 ,除数为0,竟然报wa,没报RE 
            new_len = q.top();      
        while(q.size()>1)
        {
            int a = q.top();
            q.pop();
            int b = q.top();
            q.pop();
            q.push(a+b);
            new_len += a+b;
        }
        q.pop();
        cout<<old_len<<" "<<new_len<<" "<<fixed<<setprecision(1)<<(double)old_len/new_len<<endl;
        //printf("%d %d %.1f\n",old_len,new_len,old_len*1.0/new_len);
    }
     return 0;
}
         
        
        
        
        

 

目录
相关文章
|
存储
POJ 1012 Joseph
Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53862   Accepted: 20551 Description The Joseph's problem is notoriously known.
818 0
|
测试技术
POJ 1001
此题用最朴素的思路实现即可,需模拟加法器,乘法器,最烦人的地方是特殊情形,如末位是小数点(12.^2=144,取小数点),整数末位是0(100^2=10000),0次幂,测试用例可能超出题目中说的范围,可能包含0次幂(100.0^0=0, 0.10^1=0.1)。
729 0
|
人工智能
POJ 2531
初学dfs参考别人代码,如有雷同,见怪不怪。#include using namespace std; int aa[25][25]; int maxa=0; int step[25]={0},n; void dfs(int a,int b) { int t=b; step...
676 0
|
并行计算 网络架构
poj-1005-l tanink i need a houseboat
Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned ...
940 0
|
机器学习/深度学习