光是看明白题目就得耗费不少精力,实质上就是求出ABCDE的所有排列(120个)中,与给出的n个排列中,所有字母的相对关系(10个)相异程度最低的那个。那么首先就是找出这120个排列,然后进行比较就可以了,取相异程度最小的那个输出就可以了。
Run Time: 0.01sec
Run Memory: 312KB
Code length: 1065Bytes
SubmitTime: 2011-12-25 17:39:48
// Problem#: 1006
// Submission#: 1126063
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int n;
int i, j, k;
int min;
string info;
string ranking[ 120 ];
string s = "ABCDE";
ranking[ 0 ] = s;
for ( i = 1; next_permutation( s.begin(), s.end() ); i++ )
ranking[ i ] = s;
while ( cin >> n && n ) {
int value[ 120 ] = { };
while ( n-- ) {
cin >> info;
for ( i = 0; i < 120; i++ ) {
for ( j = 0; j < 5; j++ ) {
for ( k = j + 1; k < 5; k++ ) {
if ( info.find( ranking[ i ][ j ] ) > info.find( ranking[ i ][ k ] ) )
value[ i ]++;
}
}
}
}
min = 0;
for ( i = 1; i < 120; i++ ) {
if ( value[ min ] > value[ i ] )
min = i;
}
cout << ranking[ min ] << " is the median ranking with value " << value[ min ] << ".\n";
}
return 0;
}