sicily 1006 team rankings 枚舉解題

1006. Team Rankings

Constraints

Time Limit: 1 secs, Memory Limit: 32 MBios

Description

It's preseason and the local newspaper wants to publish a preseason ranking of the teams in the local amateur basketball league. The teams are the Ants, the Buckets, the Cats, the Dribblers, and the Elephants. When Scoop McGee, sports editor of the paper, gets the rankings from the selected local experts down at the hardware store, he's dismayed to find that there doesn't appear to be total agreement and so he's wondering what ranking to publish that would most accurately reflect the rankings he got from the experts. He’s found that finding the median ranking from among all possible rankings is one way to go. 

The median ranking is computed as follows: Given any two rankings, for instance ACDBE and ABCDE, the distance between the two rankings is defined as the total number of pairs of teams that are given different relative orderings. In our example, the pair B, C is given a different ordering by the two rankings. (The first ranking has C above B while the second ranking has the opposite.) The only other pair that the two rankings disagree on is B, D; thus, the distance between these two rankings is 2. The median ranking of a set of rankings is that ranking whose sum of distances to all the given rankings is minimal. (Note we could have more than one median ranking.) The median ranking may or may not be one of the given rankings. 

Suppose there are 4 voters that have given the rankings: ABDCE, BACDE, ABCED and ACBDE. Consider two candidate median rankings ABCDE and CDEAB. The sum of distances from the ranking ABCDE to the four voted rankings is 1 + 1 + 1 + 1 = 4. We'll call this sum the value of the ranking ABCDE. The value of the ranking CDEAB is 7 + 7 + 7 + 5 = 26. 

It turns out that ABCDE is in fact the median ranking with a value of 4. 
數組

Input

There will be multiple input sets. Input for each set is a positive integer n on a line by itself, followed by n lines (n no more than 100), each containing a permutation of the letters A, B, C, D and E, left-justified with no spaces. The final input set is followed by a line containing a 0, indicating end of input.app

Output

Output for each input set should be one line of the form: 

ranking is the median ranking with value value. 

Of course ranking should be replaced by the correct ranking and value with the correct value. If there is more than one median ranking, you should output the one which comes first alphabetically. 
ide

Sample Input

4
ABDCE
BACDE
ABCED
ACBDE
0

Sample Output

ABCDE is the median ranking with value 4.

#include <iostream>
#include <map>
#include <vector>
using namespace std;

//計算對應單個rankings的一個medianrank的distance 
int rankdistance(string rank, map<char, int> order) {
	int sub_value = 0;
	
	for (int i = 0; i < rank.size(); i++) {
		for (int j = i+1; j < rank.size(); j++) {
			if (order[rank[i]] > order[rank[j]])//隊排名越前,order[rank[i]]的值越小,此處爲若單個rankings中的某個隊排名比medianrank的高,則產生distanc 
			  	sub_value++;
		}
	}
	
	return sub_value;
}
//計算對應一個rankings數組的一個medianrank的value 
int rankvalue(vector<string> rankings, string rank) {
	int value = 0;
	
	for (int i = 0; i < rankings.size(); i++) {//對每一個rankings數組的rank對medianrank計算其sub_value,累加成對因此rankings的value 
		map<char, int> order; //此步很關鍵,使用map來記錄rankings數組中的每一個rank中的每一個隊的排名,以便計算distance 
		for (int j = 0; j < rank.size(); j++) {
			order[rankings[i].at(j)] = j;
		}
		value += rankdistance(rank, order);
	}
	
	return value;
}
//枚舉全部可能的medianrank並計算其對應一個rankings數組的value,同時選擇value值最小且字母順序最小的medianrank 
void perminate(vector<string> rankings, string source, int &minvalue, string &result, int index) {//記得minvalue和result要設爲引用類型,才能使43,44行起做用 
	if (index == source.length()-1) {
		string temp;
		for (int i = 0; i < source.length(); i++) {
			temp += source[i];
		}
		int tempvalue = rankvalue(rankings, temp);
		//對全部可能的medinarank的value進行比較,選擇value最小的medianrank ,此處使用<而非<=保證了相同的value的medianrank選擇字母順序最小的 
		if (tempvalue < minvalue) {
			minvalue = tempvalue;
			result = temp;
		}
		
	} else {
		//因爲程序執行時輸入爲ABCDE做爲source,因此如下實現的該ABCDE五個字母的全排列爲升序的 
		for (int i = index; i < source.length(); i++) {
			swap(source[index], source[i]);
			perminate(rankings, source, minvalue, result, index+1);
			swap(source[index], source[i]);
		}
	}
}
int main() {
	int cases;
	while (cin >> cases && cases) {
		string str;
		vector<string> rankings;
		for (int i = 0; i < cases; i++) {
			cin >> str;
			rankings.push_back(str);
		}
		string source = "ABCDE";
		int minvalue = 1000;//因爲5個字母的最大distance爲10,因此minvalue的初始值設爲很大 
		string result;
		perminate(rankings, source, minvalue, result, 0);
		cout << result << " is the median ranking with value " << minvalue <<"." <<endl;
	}
	return 0;
} 
相關文章
相關標籤/搜索