Sicily 1006. Team Rankings

http://soj.me/1006


数据太小,直接暴力....


这是 next_permutation用法 : http://www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_permutation


#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
	int n,len = 5;
	string str[101];
	int pos[6]; //pos 记录每一个排列的字母位置
	while(cin>>n,n)
	{
		string ans;
		for(int i = 0;i < n;++i)
			cin>>str[i];
		char next[] = "ABCDE";int min = 1000;
		do
		{
		    for(int i = 0;i < len;++i)
			    pos[next[i] - 'A'] = i;
		    int value = 0;
		    for(int i = 0;i < n;++i)
		    {
				//对每一个 Ranking,检查不一样先后顺序的字母对数
			    for(int j = 0;j < len;++j)
				    for(int l =j + 1;l < len;++l)
					    if(pos[str[i][j] - 'A'] > pos[str[i][l] - 'A']) value++; 
		    }
			if(value < min)
			{
				min = value;
				ans = next;
			}
		}while(next_permutation(next,next + 5));
		cout<<ans<<" is the median ranking with value "<<min<<"."<<endl;
	}
	return 0;
}


你可能感兴趣的:(字符串)