View Code of Problem 126

#include <iostream>
#include <algorithm>
using namespace std;
struct node{
	string s;
	int k,id;
};
node dna[55];
bool cmp(node x,node y){
	if(x.k==y.k)
		return x.id<y.id;
	return x.k<y.k;
}
int main(){
	int l,n;
	cin>>l>>n;
	for(int i=0;i<n;i++){
		string s;
		cin>>s;
		int len=s.length(),cn=0;
		for(int h=0;h<len-1;h++){
			for(int j=h+1;j<len;j++){
				if(s[h]>s[j])
					cn++;
			}
		}
		dna[i].s=s;
		dna[i].k=cn;
		dna[i].id=i;
	}
	sort(dna,dna+n,cmp);
	for(int i=0;i<n;i++)
		cout<<dna[i].s<<endl;
	
	return 0;
}

Double click to view unformatted code.


Back to problem 126