zoukankan      html  css  js  c++  java
  • HDU2527 哈夫曼编码

    Safe Or Unsafe

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2366    Accepted Submission(s): 977


    Problem Description
    Javac++ 一天在看计算机的书籍的时候,看到了一个有趣的东西!每一串字符都可以被编码成一些数字来储存信息,但是不同的编码方式得到的储存空间是不一样的!并且当储存空间大于一定的值的时候是不安全的!所以Javac++ 就想是否有一种方式是可以得到字符编码最小的空间值!显然这是可以的,因为书上有这一块内容--哈夫曼编码(Huffman Coding);一个字母的权值等于该字母在字符串中出现的频率。所以Javac++ 想让你帮忙,给你安全数值和一串字符串,并让你判断这个字符串是否是安全的?
     
    Input
    输入有多组case,首先是一个数字n表示有n组数据,然后每一组数据是有一个数值m(integer),和一串字符串没有空格只有包含小写字母组成!
     
    Output
    如果字符串的编码值小于等于给定的值则输出yes,否则输出no。
     
    Sample Input
    2
    12 helloworld
    66 ithinkyoucandoit
     
    Sample Output
    no
    yes
     
    Source
     
     1 //和上一题一样
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<queue>
     6 #include<vector>
     7 #include<functional>
     8 using namespace std;
     9 int a[30];
    10 char s[1003];
    11 int ans;
    12 int main()
    13 {
    14     int n,m;
    15     while(scanf("%d",&n)!=EOF)
    16     {
    17         while(n--){
    18         scanf("%d",&m);
    19         scanf("%s",s);
    20         int len=strlen(s);
    21         priority_queue<int,vector<int>,greater<int> >q;
    22         memset(a,0,sizeof(a));
    23         for(int i=0;i<len;i++)
    24         a[s[i]-'a'+1]++;
    25         for(int i=1;i<=26;i++)
    26         if(a[i]!=0)
    27         q.push(a[i]);
    28         if(q.size()==1)
    29         ans=len;
    30         else
    31         {
    32             ans=0;
    33             while(q.size()!=1)
    34             {
    35                 int x=q.top();
    36                 q.pop();
    37                 int y=q.top();
    38                 q.pop();
    39                 ans=ans+x+y;
    40                 q.push(x+y);
    41             }
    42         }
    43         if(ans>m) printf("no
    ");
    44         else printf("yes
    ");
    45         }
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    Java 多态
    HDFS读写原理
    HDFS详解
    Servlet基础
    Tomcat
    HTTP简介
    JDBC技术
    final、finally和finalize
    java 中的权限修饰符
    进程、线程、线程状态、多线程实现方法
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6035840.html
Copyright © 2011-2022 走看看