Programming training - string sorting

Problem Description

Define the unorderedness of a string as the sum of the total number of letters after all positions that are smaller than the letters at this position. For example, the disorder degree of the string "DAABEC'' is 5, because there are 4 positions after D that are smaller than it (AABC), 1 position after E is smaller than it (C), and there are no positions smaller than itself after other positions. "AACEDGG" has a degree of disorder of 1 (the E is followed by a D smaller than it). The degree of disorder of "ZWQM" is 6, and all letters after each position are smaller than it. Now your task is to give
some Strings (consisting only of uppercase letters), sort them according to the degree of disorder from small to large, and if the degree of disorder is the same, then sort according to the relative order of the input.

input form

A single set of test data.
The first line has two integers n (0 < n <= 50) and m (0 < m <= 100), respectively representing the length of the input string and the number of strings.
Next m lines, each line contains a string of length n, consisting only of uppercase letters.

output form

Output m lines, representing the sorted strings.

sample input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

sample output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

code display

代码如下
#include<bits/stdc++.h>
using namespace std;
struct the_str{
    
    
	string s;
	int sum;
};
int main()
{
    
    
	int n,m;
	cin>>n>>m;
	the_str str[m];
	for(int i=0;i<m;i++)
	cin>>str[i].s;
	for(int i=0;i<m;i++)
	{
    
    
		string a=str[i].s;
		int num=0;
		for(int j=0;j<n;j++)
		{
    
    
			for(int k=j;k<n;k++)
			{
    
    
				if(a[j]>a[k])
				num++;
			}
			str[i].sum=num;
		}
	 }
	 for(int i=0;i<m-1;i++)
	 for(int j=0;j<m-i-1;j++)
	 {
    
    
	 	if(str[j].sum>str[j+1].sum)
	 	{
    
    
	 		the_str temp;
	 		temp=str[j];
	 		str[j]=str[j+1];
	 		str[j+1]=temp;
		 }
	 }
	 for(int i=0;i<m;i++)
	 cout<<str[i].s<<endl;
	
	
}

Guess you like

Origin blog.csdn.net/weixin_51295681/article/details/118518855