题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2527
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include<stack>
#include<queue>
using namespace std;
char s[10010];
int haf[10010];
int T;
int n;
struct cmp
{
bool operator ()(int &a,int &b)
{
return a>b;//最小值优先
}
};
priority_queue<int,vector<int>,cmp>q;
void init()
{
int i;
while(!q.empty())
q.pop();
memset(haf,0,sizeof(haf));
for(i=0;i<strlen(s);i++)
{
haf[s[i]-'a']++;
}
for(i=0;i<26;i++)
{
if(haf[i])
q.push(haf[i]);
}
}
void Haffman()
{
int a,b;
int sum=0;
while(q.size()!=1)
{
a=q.top();
q.pop();
b=q.top();
q.pop();
sum+=(a+b);
q.push(a+b);
}
printf(sum<=n?"yes\n":"no\n");
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
scanf("%s",s);
init();
if(q.size()==1)
{
int x=q.top();
q.pop();
printf(x<=n?"yes\n":"no\n");
// continue;
}
else
Haffman();
}
return 0;
}
Hdu 2527 Safe Or Unsafe-哈夫曼编码+优先队列
最新推荐文章于 2022-10-25 01:51:29 发布