• hdu 2527 Safe Or Unsafe (优先队列实现Huffman)


    Safe Or Unsafe
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3443    Accepted Submission(s): 1415

    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

    C/C++:

     1 #include <map>
     2 #include <queue>
     3 #include <cmath>
     4 #include <vector>
     5 #include <string>
     6 #include <cstdio>
     7 #include <cstring>
     8 #include <climits>
     9 #include <iostream>
    10 #include <algorithm>
    11 #define INF 0x3f3f3f3f
    12 using namespace std;
    13 const int MAX = 20;
    14 
    15 int t, n;
    16 string ss;
    17 
    18 int huffman()
    19 {
    20     int sum = 0, L[26] = {0}, len = ss.size(), now;
    21     priority_queue <int, vector <int>, greater<int> > Q;
    22     for (int i = 0; i < len; ++ i)
    23         L[ss[i] - 'a'] ++;
    24     for (int i = 0; i < 26; ++ i)
    25         Q.push(L[i]);
    26     while (Q.size() > 1)
    27     {
    28         now = Q.top(), Q.pop();
    29         now += Q.top(), Q.pop();
    30         Q.push(now);
    31         sum += now;
    32     }
    33     return sum;
    34 }
    35 
    36 int main()
    37 {
    38     scanf("%d", &t);
    39     while (t --)
    40     {
    41         scanf("%d", &n);
    42         cin >>ss;
    43         if (huffman() <= n) printf("yes
    ");
    44         else printf("no
    ");
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    使用kbmmw 生成REST 服务OpenAPI函数原型
    kbmmw 5.08 正式发布
    在datasnap 中使用unidac 访问数据(客户端)
    使用双引擎,让kbmmw 的客户端访问更方便
    使用kbmMWConfiguration 让 kbmmw smartservice 更聪明
    CE lua脚本
    error LNK2019: 无法解析的外部符号 __vsnwprintf,该符号在函数 "long __stdcall StringVPrintfWorkerW
    安装 directx sdk 出现 S1023 解决
    dx11的一些数据结构
    git 比较 change to be committed
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9548859.html
Copyright © 2020-2023  润新知