View Code of Problem 126

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
struct num {
	string s;
	int reverse;
	int id;
 
};
bool cmp(num n1, num n2) {
	if (n1.reverse < n2.reverse)return true;
	else if (n1.reverse == n2.reverse&&n1.id < n2.id)return true;
	else return false;
}
int main()
{
	int n, m;
	char arr[1000];
	num temp[1000];
	map<int, string>map;
	while (scanf("%d%d", &n, &m) != EOF) {
		for (int idx = 0; idx < m; idx++) {
			int reverseNum = 0;
			scanf("%s", arr);
			string str = arr;
			for (int i = 0; i < n; i++) {
				for (int j = i + 1; j < n; j++) {
					if (str[i] > str[j]) {
						reverseNum++;
					}
				}
			}
			temp[idx].s = str;
			temp[idx].reverse = reverseNum;
			temp[idx].id = idx;
 
		}
		sort(temp, temp + m, cmp);
		for (int i = 0; i < m; i++) {
			printf("%s\n", temp[i].s.c_str());
		}
	}
 
	return 0;
}

Double click to view unformatted code.


Back to problem 126