标签:style blog io ar color os sp for on
一. 题意
这道题就是考排列组合吧,再来就是比较一下字符的下标算一下两个ranking的距离。然后我总结了一个排列和一个组合的实现方法,这道题直接用的是stl 里面的next_permutation,注意要排好序,好像也有一个previous_permutation的方法的,不过没用过。
二. 过程
三.
1 // 2 // main.cpp 3 // sicily-1006 4 // 5 // Created by ashley on 14-10-7. 6 // Copyright (c) 2014年 ashley. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <algorithm> 11 #include <stack> 12 using namespace std; 13 string permutations[120]; 14 string guess[100]; 15 //获得120种ABCDE的排列情况 16 void getPermutaions() 17 { 18 int counter = 0; 19 string cha = "ABCDE"; 20 string adding; 21 do { 22 adding = cha; 23 permutations[counter++] = adding; 24 } while (next_permutation(cha.begin(), cha.begin() + 5)); 25 } 26 //查找一个字符在字符串里面的下标 27 int finding(char c, string s) 28 { 29 int len = (int)s.length(); 30 for (int i = 0; i < len; i++) { 31 if (s[i] == c) { 32 return i; 33 } 34 } 35 return 0; 36 } 37 //计算两个字符串的差值 38 int compare(string ss, string r) 39 { 40 int len = (int)ss.length(); 41 int sum = 0; 42 for (int i = 0; i < len - 1; i++) { 43 for (int j = i + 1; j < len; j++) { 44 if (finding(r[i], ss) > finding(r[j], ss)) { 45 sum++; 46 } 47 } 48 } 49 return sum; 50 } 51 int main(int argc, const char * argv[]) 52 { 53 int cases; 54 getPermutaions(); 55 while (cin >> cases) { 56 if (cases == 0) { 57 break; 58 } 59 int min = 25 * cases; 60 int key = -1; 61 for (int i = 0; i < cases; i++) { 62 cin >> guess[i]; 63 } 64 for (int i = 0; i < 120; i++) { 65 int diff = 0; 66 for (int j = 0; j < cases; j++) { 67 diff = diff + compare(guess[j], permutations[i]); 68 } 69 if (diff < min) { 70 key = i; 71 min = diff; 72 } 73 } 74 cout << permutations[key] << " is the median ranking with value " << min << "." << endl; 75 } 76 return 0; 77 }
源码
标签:style blog io ar color os sp for on
原文地址:http://www.cnblogs.com/ashley-/p/4151761.html