View Code of Problem 126

#include<bits/stdc++.h>
using namespace std;
#define N 55
typedef struct {
	string s;
	int num;
}Str;
int count(string str) {
	int cnt=0;
	for(int i=0;i<str.length();i++) 
		for(int j=i+1;j<str.length();j++) {
			if(str[i]>str[j])
				cnt++;
		}
	
	return cnt;
}
int mycmp(Str sa,Str sb) {
	if(sa.num!=sb.num) 
		return sa.num<sb.num;
}
int main()
{
	int n,m;
	Str str[N];
	while(cin>>n>>m) {
		for(int i=0;i<m;i++) {
			cin>>str[i].s;
			str[i].num=count(str[i].s) ;
		}
//		for(int i=0;i<m;i++)
//			cout<<str[i].s<<" "<<str[i].num<<endl;
		sort(str,str+m,mycmp);
		for(int i=0;i<m;i++)
			cout<<str[i].s<<endl;
//			cout<<str[i].s<<" "<<str[i].num<<endl;

	}
	return 0;
}

Double click to view unformatted code.


Back to problem 126