Safe Or Unsafehttp://acm.hdu.edu.cn/showproblem.php?pid=2527
Time
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1653 Accepted Submission (s): 662
problem Descriptionjavac++ One day when I was reading computer books, I saw an interesting thing! Each string of characters can be encoded into numbers to store information, but different encoding methods get the same storage space! And when the storage space is greater than a certain value is not safe! So javac++ is wondering if there is a way to get the minimum space value for the character encoding! Obviously this is possible, because there is this piece of content in the book-Huffman Code (Huffman Coding); The weight of a letter equals the frequency at which the letter appears in the string. So Javac++ want your help, give you a safe value and a string of strings, and let you know if this string is safe?
Inputinput has multiple sets of case, first, a number n means there are n sets of data, and then each set of data is a number M (integer), and a string of strings without a space only contains lowercase letters!
Outputoutput Yes if the encoded value of the string is less than or equal to the given value, otherwise the output is no.
Sample Input
212helloworld66ithinkyoucandoit
Sample Output
Noyes
SourceHDU 2008-10 Programming Contest
recommendGaojie | We have carefully selected several similar problems for you:2526 2522 2523 2524 2525
Idea: Huffman tree, take out a bunch of two weights in the smallest, synthesize a new, and then add this new formation into the string, and then operate ....
How to use priority queues in STL:
[CPP]View Plaincopy
- #include <queue> &NBSP;&NBSP;
- priority_queue<< Span class= "Datatypes" style= "margin:0px; padding:0px; Border:none; Color:rgb (46,139,87); Font-weight:bold; Background-color:inherit ">int ,&NBSP;VECTOR< int >,&NBSP;GREATER< Span class= "Datatypes" style= "margin:0px; padding:0px; Border:none; Color:rgb (46,139,87); Font-weight:bold; Background-color:inherit ">int >> Q;
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
const int m=10001;
Char s[m];
int haffman[m];
int n;
Priority_queue<int,vector<int>,greater<int> priority queue in >q;//stl
void init ()
{
While (! Q.empty ())
Q.pop ();
memset (haffman,0,sizeof (Haffman));
int Len=strlen (s), I;
for (i=0;i<len;i++)
haffman[s[i]-' a ']++;
For (i=0;i<26;i++)
Q.push (Haffman[i]);
}
void Haffman ()
{
int ans=0;
int a,b;//Constructs the thought of Huffman Tree
While (Q.size ()!=1) //Take out a bunch of two weights in the smallest, synthesize a new one, and then add the newly formed into that string, and then manipulate it .
{
a=q.top ( );
Q.pop ( );
b=q.top ( );
Q.pop ( );
ans+= (A+B);
Q.push (A+B);
}
if (ans<=n)
printf ("yes\n");
Else
printf ("no\n");
}
int main ()
{
int T;
scanf ("%d", &t);
While (t--)
{
scanf ("%d", &n);
scanf ("%s", s);
init ();
if (q.size () ==1)
{
int a=q.top ();
Q.pop ();
if (a<=n)
printf ("yes\n");
Else
printf ("no\n");
continue;
}
Haffman ();
}
return 0;
}
Hdoj 2527 Safe Or Unsafe (Havermann algorithm)