Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2527
Simulate with a priority queue
#include <iostream>#include<stdio.h>#include<cstring>#include<algorithm>#include<queue>using namespacestd;#defineN 1100#defineMet (A, b) memset (A, B, sizeof (a))structnode{intx; FriendBOOL operator< (Node A, Node B)///priority queue, substance is heap; { returna.x >b.x; }};intMain () {intT, N; intCnt[n]; CharS[n]; scanf ("%d", &T); while(t--) {Met (S,0); Met (CNT,0); scanf ("%d", &N); scanf ("%s", s); intLen =strlen (s); for(intI=0; i<len; i++) Cnt[s[i]-'a']++; Priority_queue<node>Q; node p, q; for(intI=0; i< -; i++) {p.x=Cnt[i]; if(Cnt[i]) Q.push (p); } intAns =0 ; if(q.size () = =1)///if only one element wpl is itself;Ans =q.top (). x; while(Q.size ()! =1) {node min1= Q.top (); Q.pop ();///find two minimum weights to form a new nodeNode min2 =Q.top (); Q.pop (); q.x= min1.x +min2.x; Q.push (q); Ans+=q.x; } ///printf ("%d\n", ans);printf (ans <= n?)"yes\n":"no\n"); } return 0;}
View Code
Safe Or unsafe--hdu2527 (Huffman tree seeking WPL)