C++ 代码:按逆序对数量排序 DNA 字符串
#include
int n, m;
int countReversePairs(string s) { int cnt = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (s[i] > s[j]) cnt++; } } return cnt; }
bool cmp(pair<string, int> a, pair<string, int> b) { if (a.second != b.second) { return a.second < b.second; } else { return false; } }
int main() { cin >> n >> m; vector<pair<string, int>> v(m); for (int i = 0; i < m; i++) { cin >> v[i].first; v[i].second = countReversePairs(v[i].first); } sort(v.begin(), v.end(), cmp); for (auto p : v) { cout << p.first << endl; } return 0; }
原文地址: https://www.cveoy.top/t/topic/oaWb 著作权归作者所有。请勿转载和采集!