WPL:weight path length 树的带权路径长度
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[30];
memset(arr, 0, sizeof(arr));
string s;
cin >> s;
for (int i = 0; i < s.length(); i++) {
arr[s[i] - 'A']++;
}
priority_queue<int, vector<int>, greater<int> > que;//优先队列greater是小的做top,>>连着会当做右移运算符
for (int i = 0; i < 30; i++) {
if(arr[i] > 0) {
que.push(arr[i]);
}
}
int sum = 0;
int ans = 0;
int flag = 1;
if (que.size() == 1) {
ans = que.top();
flag = 0;
}
while (que.size() > 1 && flag) {
int t1 = que.top();
que.pop();
int t2 = que.top();
que.pop();
sum = t1 + t2;
ans += sum;
que.push(sum);
}
cout << "WPL是" << ans << endl;
return 0;
}