杭电2527——哈夫曼编码
题目传送门
# include <iostream>
# include <algorithm>
# include <vector>
# include <stack>
# include <queue>
# include <string>
# include <sstream>
# include <cstdio>
# include <cstring>
# include <cmath>
using namespace std;
priority_queue <int, vector <int>, greater<int> > q;
int a[30];
int ans = 0;
void create() {
int a, b;
while (q.size() > 1) {
a = q.top(); q.pop();
b = q.top(); q.pop();
ans += a + b;
q.push(a + b);
}
if (ans == 0) {
ans = q.top();
}
}
int main() {
string s;
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
cin >> s;
memset(a, 0, sizeof(a));
while (!q.empty()) q.pop();
ans = 0;
for (int i = 0; i < s.size(); i++) {
a[s[i] - 'a']++;
}
for (int i = 0; i < 30; i++) {
if (a[i]) {
q.push(a[i]);
}
}
create();
if (ans <= n) {
cout << "yes" << endl;
}
else {
cout << "no" << endl;
}
}
return 0;
}