求逆序对数,数据规模小,暴力即可。
这里还用到了STL中的组合数函数next_permutation(),顺便贴上该函数的用法:http://www.cplusplus.com/reference/algorithm/next_permutation/
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <stack>
#define MIN 0x3f3f3f3f
using namespace std;
int main(){
//freopen("in.txt", "r", stdin);
int n;
while(cin >> n && n){
vector<string> team(105);
for(int i = 0; i < n; i++)
cin >> team[i];
string s = "ABCDE", ans;
int res = MIN;
do{
int cont = 0;
for(int j = 0; j < n; j++){
vector<bool> flag(10, false);
for(int i = 0; i < s.size(); i++){
for(int k = 0; k < team[j].size(); k++){
if(s[i] == team[j][k]){
flag[k] = true;
break;
}
if(!flag[k])
cont++;
}
//cout << cont << endl;
}
}
if(cont < res){
res = cont;
ans = s;
}
}while(next_permutation(s.begin(), s.end()));
cout << ans << " is the median ranking with value " << res <<"." <<endl;
}
}