poj 2038 Team Rankings 枚举排列

//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;
} 

posted @ 2016-01-08 08:03  lcchuguo  阅读(285)  评论(0编辑  收藏  举报