33 lines
944 B
C++
33 lines
944 B
C++
|
|
#include <bits/stdc++.h>
|
||
|
|
using namespace std;
|
||
|
|
const int N = 110;
|
||
|
|
struct Node {
|
||
|
|
string s;
|
||
|
|
int id, t;
|
||
|
|
} str[N];
|
||
|
|
int n, m;
|
||
|
|
|
||
|
|
bool cmp(Node a, Node b) {
|
||
|
|
if (a.t != b.t) return a.t < b.t;
|
||
|
|
return a.id < b.id;
|
||
|
|
}
|
||
|
|
int main() {
|
||
|
|
cin >> m >> n; // 字符串长度,字符串个数
|
||
|
|
for (int i = 1; i <= n; i++) {
|
||
|
|
cin >> str[i].s; // 字符串
|
||
|
|
str[i].id = i; // 序号
|
||
|
|
}
|
||
|
|
for (int i = 1; i <= n; i++) {
|
||
|
|
int t = 0;
|
||
|
|
for (int k = 0; k < m - 1; k++) // 遍历第i个字符串的每个字符
|
||
|
|
for (int j = k + 1; j < m; j++)
|
||
|
|
if (str[i].s[k] > str[i].s[j]) t++; // 统计逆序的个数
|
||
|
|
str[i].t = t; // 保存上这个属性
|
||
|
|
}
|
||
|
|
// 按逆序的个数由小到大排序
|
||
|
|
sort(str + 1, str + n + 1, cmp);
|
||
|
|
// 输出
|
||
|
|
for (int i = 1; i <= n; i++) cout << str[i].s << endl;
|
||
|
|
return 0;
|
||
|
|
}
|