Constraints
Time Limit: 1 secs, memory limit: 32 MB
Description
It's preseason and the local newspaper wants to publish a preseason ranking of the teams in the local amateur basketball leleague. the teams are the ants, the buckets, the cats, the dribblers, and
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
Wocould 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 cocould 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.
Output
Output for each input set shocould be one line of the form:
Ranking is the median ranking with value.
Of course ranking shoshould be replaced by the correct ranking and value with the correct value. If there is more than one median ranking, you shoshould output the one which comes first alphabetically.
Sample Input
4ABDCEBACDEABCEDACBDE0
Question Analysis:
Initial Error Analysis: When you first see this question, consider the relative location relationship between the two elements, such as AB, to find the AB relative relationship in each string. A has more before B, in the result, B is behind a. In the opposite result, B is before a, and then follows the fast-forward principle. After a day of test, it is okay, but it is wa. In the end, we find that A, B, C, D, and E are not passed. For example, a <B, B <C cannot judge a <C. So this method passes
One way on the Internet is to list all the columns and then find the minimum values (I think this is a poor question). However, I didn't think much about AC at last. The next_permutation function is provided for all sorting STL, and the parameter should start with ABCDE.
# Include <iostream> # include <stdio. h> # include <cmath> # include <iomanip> # include <map> # include <vector> # include <string> # include <algorithm> # include <sstream> # include <stack> using namespace STD; vector <Map <char, int> data; int N; // count int cmpint (char X1, char x2) {int sum = 0; For (INT I = 0; I <n; I ++) {If (data [I] [X1]> data [I] [X2]) sum ++;} return sum;} int main () {While (CIN> N & n! = 0) {data. clear (); data. resize (n); For (INT I = 0; I <n; I ++) {string TMP; CIN> TMP; For (Int J = 0; j <TMP. size (); j ++) data [I]. insert (make_pair (TMP [J], j);} // end forvector <char> result; For (INT I = 0; I <5; I ++) result. push_back ('A' + I); int min = 0; For (INT I = 0; I <5; I ++) {for (Int J = I + 1; j <5; j ++) min + = cmpint (result [I], result [J]);} vector <char> TMP = result; while (next_permutation (result. begin (), result. end () {int sum = 0; For (INT I = 0; I <5; I ++) {for (Int J = I + 1; j <5; j ++) sum + = cmpint (result [I], result [J]);} If (sum <min) {min = sum; TMP = result ;}} for (INT I = 0; I <5; I ++) cout <TMP [I]; cout <"is the median ranking with value" <min <". "; cout <Endl;} // end while}