| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 79776 | Accepted: 32061 |
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.
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.
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 (0 < 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''. Since two strings can be equally sorted, then output them according to the orginal order.
Sample Input
10 6 AACATGAAGG TTTTGGCCAA TTTGGCCAAA GATCAGATTT CCCGGGGGGA ATCGATGCAT
Sample Output
CCCGGGGGGA AACATGAAGG GATCAGATTT ATCGATGCAT TTTTGGCCAA TTTGGCCAAA
Source
题目应该是挺简单的,所以还是比较适合我这种新手的。但是,因为在循环中一个小细节的错误,导致我花了挺长时间来找这个bug的。但是,以后也有了一点经验。先贴一下原来的有bug的代码吧
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 using std::string; 5 const int NUM = 100; 6 const int MAX = 1000; 7 int main() 8 { 9 string str[NUM];//dna数组 10 int measure[NUM];//混乱度数组 11 int length, numbers; 12 cin >> length >> numbers; 13 for (int i = 0; i != numbers; ++i){ 14 cin >> str[i]; 15 measure[i] = 0; 16 for (int j = 0; j != length; ++j){ 17 for (int k = j + 1; k != length; ++k){ 18 if (str[i][j] > str[i][k]) 19 ++measure[i]; 20 } 21 } 22 } 23 for (int count = 0; count != numbers; ++count){ 24 int temp = measure[0]; 25 int pos = 0; 26 for (int index = 1; index != numbers; ++index){ 27 if (temp > measure[index]){ 28 temp = measure[index]; 29 ++pos;//记录混乱度最小的dna的下标 30 } 31 } 32 measure[pos] = MAX;//将已找到混乱度最小的dna的混乱度该为最大,方便第n次循环找第n大的混乱度 33 cout << str[pos] << endl; 34 } 35 system("pause"); 36 return 0; 37 }
不知道下次能不能直接找到这个bug。这个bug给我的经验是,在循环中的变量很有可能会出错,所以要么编写的时候深思熟虑,要么debug的时候直接将循环演示一遍,那就一目了然了。
1 for (int index = 1; index != numbers; ++index){ 2 if (temp > measure[index]){ 3 temp = measure[index]; 4 pos = index;//记录混乱度最小的dna的下标 5 } 6 }
这是正确的代码。记录这题的另一个目的是,以后若是完整的学习了算法和数据结构,可以试试能不能将它优化一下。
刚刚查了一下别人的源码,发现有一种qsort(),明天要好好学习一下!
keep coding!
浙公网安备 33010602011771号