View Code of Problem 126

#include<iostream>
#include<map>
#include<algorithm>
using namespace std;

bool cmp(pair <string,int> &a, pair<string,int> &b){
     return a.second < b.second;
}

int main()
{
    int n,m;
    cin >> n >> m;
    pair<string ,int > a[n];
    for(int i = 0; i < m; i++){
        cin >>a[i].first;
        a[i].second = 0;
        for(int j = 0; j < n - 1; j++)
            for(int k =j + 1; k < n; k++)
               if(a[i].first[j] > a[i].first[k])
                 a[i].second++;
    }
    sort(a , a + m,cmp);
    for(int i = 0; i < m; i++){
        cout << a[i].first <<endl;
    }

}

Double click to view unformatted code.


Back to problem 126