#include <stdio.h>
#include <stdlib.h>
#define MAX_CHARS 50
#define MAX_STRINGS 100
struct DNA{
char ch[MAX_CHARS];
int inversions;
};
int cmp(const void *a ,const void *b);
int main(void)
{
int chars, strings;
int i, j;
int num_A, num_C, num_G, num_T;
struct DNA dna[MAX_STRINGS];
if(scanf("%d %d", &chars, &strings))
{
for(i = 0; i < strings; ++i) //输入数据
{
getchar();
for(j = 0; j < chars; ++j)
dna[i].ch[j] = getchar();
}
for(i = 0; i < strings; ++i) //计算逆序数
{
num_A = num_C = num_G = num_T = 0;
dna[i].inversions = 0;
for(j = 0; j < chars; ++j)
{
switch(dna[i].ch[chars - j -1])
{
case 'A': num_A++; break;
case 'C': num_C++; dna[i].inversions += num_A;break;
case 'G': num_G++; dna[i].inversions += (num_A + num_C);break;
case 'T': num_T++; dna[i].inversions += (num_A + num_C + num_G);break;
default : break;
}
}
}
qsort(dna, strings, sizeof(dna[0]), cmp); //排序
for(i = 0; i < strings; ++i) //输出
{
for(j = 0; j < chars; ++j)
{
putchar(dna[i].ch[j]);
}
printf("\n");
}
}
return 0;
}
int cmp(const void *a ,const void *b)
{
return (*(structDNA *)a).inversions > (*(structDNA *)b).inversions ?1 : -1;
}