[Huffman]Hdu 2527 Safe Or Unsafe

 

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <queue>

 4 #include <cstring>

 5 #include <string>

 6 using namespace std;

 7 

 8 int t,len,k,num,sum;

 9 int a[27];

10 bool vis[27];

11 string str;

12 priority_queue<int, vector<int>,greater<int> > pq;

13 

14 int main(){

15 

16     scanf("%d",&t);

17     while(t--){

18         num=0;

19         sum=0;

20         memset(a,0,sizeof a);

21         memset(vis,0,sizeof vis);

22         while(!pq.empty())pq.pop();

23 

24         

25         scanf("%d",&k);

26         cin>>str;

27 

28         len=str.length();

29         for(int i=0;i<len;i++){

30             a[ str[i]-'a']++;

31             vis[ str[i]-'a']=1;

32         }

33         for(int i=0;i<=25;i++){

34             if(vis[i]){

35                 pq.push(a[i]);

36                 num++;

37             }

38         }

39         if(num==1)

40             sum=pq.top(); //Pay attention to the situation !

41         else{

42             for(int i=1;i<num;i++){

43                 int t1=pq.top();pq.pop();

44                 int t2=pq.top();pq.pop();

45                 sum+=t1+t2;pq.push(t1+t2);

46             }

47             

48         }

49 

50         if(sum<=k)printf("yes\n");

51         else printf("no\n");

52     }

53 }

 

你可能感兴趣的:(Huffman)