zoj 1188 DNA Sorting
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
注意输出格式,输出的每一个块后需要空一行,最后一个块后不空行
多组测试用例,容器每次要清零
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 bool cmp(const string &s1, const string &s2){ 7 int i, j; 8 int l1 = s1.length(), l2 = s2.length(); 9 int c1 = 0, c2 = 0; 10 for(i = 0; i < l1 - 1; i++){ 11 for(j = i + 1; j < l1; j++){ 12 if(s1[i] > s1[j]) 13 c1++; 14 } 15 } 16 for(i = 0; i < l2 - 1; i++){ 17 for(j = i + 1; j < l2; j++){ 18 if(s2[i] > s2[j]) 19 c2++; 20 } 21 } 22 //如果逆序数相同,那么会按原先固有的位置排序 23 return c1 < c2; 24 } 25 26 int main(){ 27 int t; 28 int n, m; 29 string str; 30 vector<string> v; 31 cin >> t; 32 while(t--){ 33 cin >> n >> m; 34 v.clear(); 35 for(int i = 0; i < m; i++){ 36 cin >> str; 37 v.push_back(str); 38 } 39 sort(v.begin(), v.end(), cmp); 40 for(vector<string>::iterator it = v.begin(); it != v.end(); it++){ 41 cout << *it << endl; 42 } 43 if(t != 0) 44 cout << endl; 45 } 46 //system("pause"); 47 return 0; 48 }