直接优先队列模拟就可以了,上代码,这里q.size()==1WA了一次,直接只想到len==1的特殊情况
#include <map>
#include <set>
#include <list>
#include <cmath>
#include<cctype>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b)
{
return a % b == 0 ? b : gcd(b, a % b);
}
#define MAXN 5005
int vis[30];
priority_queue<int,vector<int> ,greater<int> >q;
char str[MAXN];
int len;
void init()
{
memset(vis,0,sizeof(vis));
while (!q.empty()) q.pop();
len=strlen(str);
for (int i=0;i<len;i++)
{
if (str[i]=='_') vis[26]++;
else vis[str[i]-'A']++;
}
for (int i=0;i<=26;i++)
if (vis[i]) q.push(vis[i]);
}
void slove()
{
int ans=0;
while (!q.empty())
{
int t1=q.top();q.pop();
if (q.empty()) break;
int t2=q.top();q.pop();
ans+=t1+t2;
q.push(t1+t2);
}
printf("%d %d %.1f\n",8*len,ans,(double)8*len/ans);
}
int main()
{
// freopen("sample.txt","r",stdin);
while (scanf("%s",str)!=EOF)
{
if (strcmp(str,"END")==0) break;
init();
if (q.size()==1) {printf("%d %d 8.0\n",len*8,len);continue;}
slove();
}
return 0;
}