Safe Or unsafe--hdu2527 (Huffman tree seeking WPL)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.