zoj 2207 Team Rankings

来源:互联网 发布:淘宝详情页教程视频 编辑:程序博客网 时间:2024/06/14 06:24
//由字符串“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;}}

原创粉丝点击