#include #include #include using namespace std;

// 求逆序对数 int getReversePairs(string& s) { int n = s.size(); int cnt = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (s[i] > s[j]) { cnt++; } } } return cnt; }

// 自定义比较函数,按逆序对数从小到大排序 bool cmp(string& s1, string& s2) { int cnt1 = getReversePairs(s1); int cnt2 = getReversePairs(s2); if (cnt1 != cnt2) { return cnt1 < cnt2; } else { return s1 < s2; } }

int main() { int n, m; cin >> n >> m; vector strs(m); for (int i = 0; i < m; i++) { cin >> strs[i]; } sort(strs.begin(), strs.end(), cmp); for (auto& s : strs) { cout << s << endl; } return 0;


原文地址: https://www.cveoy.top/t/topic/fjso 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录