每加一次相当于多一位编码。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int N = 50;
const int INF = 1000000;
struct node
{
int w;
friend bool operator < (const node &a, const node &b)
{
return a.w > b.w;
}
};
int main()
{
// freopen("in.txt", "r", stdin);
char s[5000];
int a[30];
int ans, C, num;
scanf("%d", &C);
while(C --)
{
cin >> num;
scanf("%s", s);
ans = 0;
int len = strlen(s);
memset(a, 0, sizeof(a));
for(int i = 0; i < len; i ++)
a[s[i] - 'a' + 1] ++;
priority_queue <node> q;
for(int i = 0; i < 27; i ++)
{
node tmp;
tmp.w = a[i];
if(tmp.w) q.push(tmp);
}
if(q.size() == 1) ans = len;//只有一种字符,每个字符一个字节,共len个
else
{
while(q.size() > 1)
{
node tmp2, tmp3, tmp0;
tmp2 = q.top();
q.pop();
tmp3 = q.top();
q.pop();
tmp0.w = tmp2.w + tmp3.w;
ans += tmp2.w + tmp3.w;
q.push(tmp0);
}
}
if(ans < num) printf("yes\n");
else printf("no\n");
}
return 0;
}