View Code of Problem 126

#include<bits/stdc++.h>
using namespace std;
typedef struct dna {
	char s[100];
	int num;
}DNA;
int cou(char a[]) {
	int count = 0;
	for (int i = 0; i < strlen(a); i++) {
		for (int j = i + 1; j < strlen(a); j++) {
			if (a[i] > a[j])count++;
		}
	}
	return count;
}
bool cmp(dna a, dna b) {
	return a.num < b.num;
}
int main() {
	int m, n;
	while (scanf("%d %d", &m, &n) != EOF) {
		DNA s1[100];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)cin >> s1[i].s[j];
		}
		for (int k = 0; k < n; k++) {
			s1[k].num = cou(s1[k].s);
		}
		stable_sort(s1, s1 + n, cmp);
		for (int i = 0; i < n; i++)cout << s1[i].s << endl;
	}

}

Double click to view unformatted code.


Back to problem 126