暴力+模拟=水
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int tot[50];
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
memset(tot,0,sizeof(tot));
char s[10];
for(int i=0;i<n;i++)
{
scanf("%s",s);
for(int j=0;j<5;j++)
for(int k=j+1;k<5;k++)
tot[(s[j]-'A')*10+s[k]-'A']++;
}
char ans[]={"ABCDE"},temp[]={"ABCDE"};
int val=0;
for(int i=0;i<5;i++)
for(int j=i+1;j<5;j++)
val+=n-tot[(ans[i]-'A')*10+ans[j]-'A'];
while(next_permutation(temp,temp+5))
{
int tp=0;
for(int i=0;i<5;i++)
for(int j=i+1;j<5;j++)
tp+=n-tot[(temp[i]-'A')*10+temp[j]-'A'];
if(tp<val)
{
val=tp;
strcpy(ans,temp);
}
}
printf("%s is the median ranking with value %d.\n",ans,val);
}
return 0;
}