思路:
对每一个输入项进行计算,有多少个逆序,然后按照逆序数为主要排列,如果逆序数一样,就按照一开始的序号排序
代码如下:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
struct ss
{
char s[200];
int n;
int t;
}str[110];
bool cmp(ss a, ss b)
{
if (a.n == b.n)
return a.t < b.t;
else
return a.n < b.n;
}
int main()
{
int c;
int n, m;
int i;
int j, k;
cin >> c;
while (c--)
{
memset(str, 0, sizeof(str));
cin >> n >> m;
getchar();
for (i = 0; i < m; i++)
{
cin >> str[i].s;
str[i].t = i;
for (j = 0; j < n; j++)
{
for (k = j; k < n; k++)
{
if (str[i].s[j] > str[i].s[k])
str[i].n++;
}
}
}
sort(str, str + m, cmp);
for (i = 0; i < m; i++)
{
printf("%s\n", str[i].s);
}
}
system("pause");
return 0;
}