poj 2038 Team Rankings 枚举排列

时间:2021-02-19 01:37:43
//poj 2038
//sep9
#include <iostream>
#include <algorithm>
using namespace std;
char s[128][8];
int count(char s1[],char s2[])
{
int cnt=0;
for(int i=0;i<5;++i)
for(int j=i+1;j<5;++j){
int k;
for(k=0;k<5;++k)
if(s1[i]==s2[k])
break;
for(int l=0;l<k;++l)
if(s1[j]==s2[l])
++cnt;
}
return cnt;
}
int main()
{
int n;
while(scanf("%d",&n)==1&&n){
for(int i=0;i<n;++i)
scanf("%s",s[i]);
int ans=INT_MAX;
char ans_s[8];
char tmp[8]="ABCDE";
do{
int cnt=0;
for(int i=0;i<n;++i)
cnt+=count(tmp,s[i]);
if(cnt<ans){
ans=cnt;
strcpy(ans_s,tmp);
}
}while(next_permutation(tmp,tmp+5));
printf("%s is the median ranking with value %d.\n",ans_s,ans);
}
return 0;
}