zoj 2207 Team Rankings

//由字符串“ABCDE”的所有排序(可以通过stl中的next_permutation直接得出)与input中
//的字符串进行比较,找出差距值最小的字符串!
#include "iostream"
#include "map"
#include "algorithm"
#include "string"
#include "limits.h"
using namespace std;

string input[110];
map<char, int> m;

int solve(string temp)//比较两个字符串的差距值
{
	int i, j, sum = 0;
	for (i = 0; i < 4; i++)
		for (j = i + 1; j < 5; j++)
		{
			if (m[temp[i]] > m[temp[j]])
				sum++;
		}
	return sum;
}

int main()
{
	int n, i, count, sum;
	string first, temp;
	while (cin >> n && n)
	{
		for (i = 0; i < n; i++)
			cin >> input[i];
		first = "ABCDE";
		count = INT_MAX;
		do
		{
			for (i = 0; i < 5; i++)
				m[first[i]] = i + 1;
			sum = 0;
			for (i = 0; i < n; i++)
				sum += solve(input[i]);
			if (count > sum)
			{
				count = sum;
				temp = first;
			}
		}while (next_permutation(first.begin(), first.begin() + 5));
		//由此函数可以生成字符串“ABCDE”的所有排序!
		cout << temp << " is the median ranking with value " << count << "." << endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值