View Code of Problem 126

#include <iostream>
#include <algorithm>
using namespace std;

typedef struct{
    char num[51];
    int nxs=0;
}dna;

bool cmp(dna a,dna b){
    return a.nxs < b.nxs;
}

int main(){
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF){
        dna d[m];
        for(int i=0;i<m;i++){
            scanf("%s",d[i].num);
            for(int j=0;j<n;j++){
                for(int k=j+1;k<n;k++){
                    if(d[i].num[j]>d[i].num[k])
                        d[i].nxs ++;
                }
            }
        }
        
        sort(d,d+m,cmp);
        for(int j=0;j<m;j++){
            printf("%s\n",d[j].num);
        }
    }
    return 0;
}

Double click to view unformatted code.


Back to problem 126