View Code of Problem 126

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

typedef struct dna{
	string str;
	int count;
}dna;
bool cmp(dna a,dna b);
int main() {
	int n,m;
	while(cin >>n>>m){
		dna dna[m];
		for(int i=0;i<m;i++){
			cin >>dna[i].str;
			dna[i].count=0;
		}
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				for(int k=j;k<n;k++){
					if(dna[i].str[j]>dna[i].str[k]){
						dna[i].count++;
						//cout <<dna[i].str[j]<<" "<<dna[i].str[k]<<endl;
					}
				}
			}
		}
		/*
		for(int i=0;i<m;i++){
			cout << dna[i].count<<endl;
		}
		*/
		stable_sort(dna,dna+m,cmp);
		for(int i=0;i<m;i++){
			cout <<dna[i].str<<endl;
		}
	}
	return 0;
}

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

Double click to view unformatted code.


Back to problem 126