View Code of Problem 143

#include<iostream>
#include<algorithm>
using namespace std;
struct node{
	string s;
	int d;
};
bool cmp(struct node x,struct node y){
	return x.d<y.d;
}
int main(){
	int n,m;
	while(cin>>n>>m){
		struct node x[m];
		for(int i=0;i<m;++i){
			cin>>x[i].s;
			x[i].d=0;
			for(int j=0;j<x[i].s.size();++j){
				for(int k=j+1;k<x[i].s.size();++k)
					if(x[i].s[j]>x[i].s[k])
					x[i].d++;	
			}	
		}
		sort(x,x+m,cmp);
		for(int i=0;i<m;++i)
		cout<<x[i].s<<endl;
	}
}

Double click to view unformatted code.


Back to problem 143