Problem Description
One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)--it is nearly sorted--while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be--exactly the reverse of sorted).
You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (1 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.
Output
Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. If two or more strings are equally sorted, list them in the same order they are in the input file.
Sample Input
1 10 6 AACATGAAGG TTTTGGCCAA TTTGGCCAAA GATCAGATTT CCCGGGGGGA ATCGATGCAT
Sample Output
CCCGGGGGGA AACATGAAGG GATCAGATTT ATCGATGCAT TTTTGGCCAA TTTGGCCAAA
看了很久都不知道这个题到底是什么意思,才知道是求每个字符串里的每一个字母后面有几个比它小的字母的总和,并对总和按照从小到大排列,题目数据量较小,直接循环就过.
注意 这道题我WA了好几次 没有弄明白 c++ sort 和 stable_sort 的区别
sort 对给定区间所有元素进行排序 stable_sort 对给定区间所有元素进行稳定排序 这道题是使用的后者 希望看见本文章的人不要跟我犯一样的错误
#include<stdlib.h>
#include<stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct T{
string s;
int count;
}a[100];
bool cmp(T a,T b){ //升序 定义规则
return a.count<b.count;
}
int main()
{
int m,n,t;
scanf("%d",&n);
while(n--){
cin>>m>>t;
for(int i=0;i<t;i++){
cin>>a[i].s;
a[i].count=0;
for(int j=0;j<=m-2;j++)//选择排序
for(int x=j+1;x<=m-1;x++)
{
if(a[i].s[j]>a[i].s[x])
a[i].count++;
}
}
stable_sort(a,a+t,cmp);
for(int y=0;y<t;y++)
cout<<a[y].s<<endl;
}
return 0;
}