作弊
哈夫曼树模板题
特判一下只有一种字母的情况(虽然测试数据并没有这种情况QWQ)
参考代码
#include<bits/stdc++.h>
using namespace std;
string a;
int ans,t[200];
priority_queue< int > v;
signed main()
{
cin>>a;
for(int i=0;i<a.length();i++) t[a[i]]++;
for(int i='a';i<='z';i++) if(t[i]) v.push(-t[i]);
if(v.size()==1)
return printf("%d\n",a.length())&0;
while(v.size()!=1){
int k=0;
k+=v.top();v.pop();
k+=v.top();v.pop();
v.push(k);ans+=k;
}
cout<<-ans<<"\n";
return 0;
}
