//题意:给一个n, m分别代表列和行数,然后要求计算每一行的逆序数,按照逆序数从小到大的顺序排列,什么是逆序数:ATCGGCA 这个序列若正常顺序是AACCGGT,此时它的下标为1的T,下标为3,4的G,下标为5的C分别没有按照要求放置,则逆序数为:T:5;G(下标为3):2; G(下标为4):2;C:1,5+2+2+1 = 10,则逆序数为10。【注意】若相同的两个逆序数序列按原来的位置放置。
//思路:计算每一行的逆序数,用sort排序,这里处理一下相同的时候,然后输出即可,具体见代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
int data;
int index;
}num[120];
bool cmp(const node a, const node b)
{
if(a.data == b.data){ //这里就是当相同的逆序数时,按照下标小的顺序输出
return a.index < b.index;
}
return a.data < b.data;
}
void cal(char **c, int a, int len)
{
int A = 0, C = 0, G = 0, T = 0, cnt = 0;
for(int i = len-1; i >= 0; i--){
switch(c[a][i]){
case 'A':
A++;
break;
case 'C':
cnt = cnt + A;
C++;
break;
case 'G':
cnt = cnt + A + C ;
G++;
break;
case 'T':
cnt = cnt + A + C + G;
break;
}
}
num[a].data = cnt;
num[a].index = a;
return ;
}
int main()
{
int n, m;
while(scanf("%d%d", &n, &m) != EOF){
char **c = new char *[m]; //这里主要想用一下将二维数组当作参数传递,则需要动态申请,可以直接全局变量更好用
for(int i = 0; i < m; i++){
c[i] = new char[n+1];
}
for(int i = 0; i < m; i++){
getchar();
for(int j = 0; j < n; j++){
scanf("%c", &c[i][j]);
}
cal(c, i, n);
}
sort(num, num+m, cmp);
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++)
printf("%c", c[num[i].index][j]);
printf("\n");
}
delete c;
}
return 0;
}