#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#define N 55
#define M 105
using namespace std;
typedef struct dna {
char str[N];
int num;//逆序对个数
}DNA;
DNA a[M];
int n, m;
int count_(char s[])//返回值为输入数组的逆序对个数
{
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;
}
int cmp(DNA x, DNA y)//按照逆序对从小到大排序
{
return x.num < y.num;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++)
{
scanf("%s", a[i].str);
a[i].num = count_(a[i].str);
}
sort(a, a + m, cmp);
for (int i = 0; i < m; i++)
printf("%s\n", a[i].str);
return 0;
}
|