Time Limit: 2 Seconds Memory Limit: 65536 KB
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.
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
Source: East Central North America 1998
该题目中仅需要改变一下string的排序方式,然后使用STL容器就可以了。不过有一点,题目中说,如果按照规定的比较方法比较的结果是一样大的话,就按照输入的顺序输出,这一点为了节省了很多麻烦。
代码如下:
#include<iostream>
#include<set>
#include<string>
#include<algorithm>
#include<list>
using namespace std;
class DNA
{
public:
string actg;
DNA(string& s):actg(s){}
bool operator<(const DNA& dna) const
{
return inversionNum() < dna.inversionNum();
}
int inversionNum() const
{
int bigToRight = 0;
for(int i = 0; i < actg.length(); i++)
{
for(int j = i + 1; j < actg.length(); j++)
{
if(actg[i] > actg[j])
bigToRight++;;
}
}
return bigToRight;
}
};
int main()
{
int blocks;cin>>blocks;
for(int block = 0; block < blocks; block++)
{
string state;
if(block == 0)//仅在第一次的时候有如下两个空行
{
getline(cin,state);//用getline 吃掉输入blocks之后的回车
getline(cin,state);//题目要求的空行
}
int n,m;
cin>>n>>m;
multiset<DNA> se;
list<string> strList;
for(int i = 0; i < m; i++)
{
string actg;cin>>actg;
se.insert(DNA(actg));
}
for(multiset<DNA>::iterator it = se.begin(); it != se.end(); it++)
{
cout<<(*it).actg<<endl;
}
if(block != blocks - 1)//最后一次输出没有空行
{
cout<<endl;
}
}
return 0;
}
完成之后,我就在想,如果按照题意中的比较方法比较的结果是相等的话,就按string默认的比较方式来排序的话,<重载的方法应该如下所示:
bool operator<(const DNA& dna) const
{
return inversionNum() < dna.inversionNum() ? true:actg<dna.actg;
}但是真的这样做的话,程序是会出错的,老是说我的<重载有错误,这我就不知道错误在哪里了,这个有待研究一下,到底我这样写回导致怎么样的行为,如果要实现我的想法的话,<的重载又该怎么写呢?