View Code of Problem 143

#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct node {
	string s;
	int num;
};
bool cmp(node a, node b)
{
	return a.num < b.num;
}
int main()
{
	int n, m;
	while (cin >> n >> m)
	{
		vector<node>a;
		while (m--)
		{
			node z;
			cin >> z.s;
			z.num = 0;
			for (int i = 0;i < n;i++)
			{
				for (int j = i + 1;j < n;j++)
				{
					if (z.s[i] > z.s[j])
					{
						z.num++;
					}
				}
			}
			a.push_back(z);
		}
		sort(a.begin(), a.end(), cmp);
		for (int i = 0;i < a.size();i++)
			cout << a[i].s << endl;
	}
 
	
	
}

Double click to view unformatted code.


Back to problem 143