View Code of Problem 126

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

struct st{
    char str[51];
    int num;
};
int cmp(struct st a,struct st b)
{
    return a.num<b.num;
}
int fun(char str[])
{
    int n=strlen(str);
    int arr=0;
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(str[i]>str[j])arr++;
        }
    }
    return arr;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        struct st p[n];
        for(int i=0;i<m;i++)
        {
            cin>>p[i].str;
            p[i].num=fun(p[i].str);
        }

        sort(p,p+m,cmp);

        for(int i=0;i<m;i++)
        {
            cout<<p[i].str<<endl;
        }
    }
    return 0;
}

Double click to view unformatted code.


Back to problem 126