通过率:39% 难度:较难
DNA Sorting
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 46899 |
|
Accepted: 18311 |
Description
One measure of``unsortedness'' in a sequence is the number of pairs of entries that are outof 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 itsright and E is greater than one letter to its right. This measure is called thenumber of inversions in the sequence. The sequence ``AACEDGG'' has only oneinversion (E and D)---it is nearly sorted---while the sequence ``ZWQM'' has 6inversions (it is as unsorted as can be---exactly the reverse of sorted).
You are responsible for cataloguing a sequence of DNA strings (sequencescontaining only the four letters A, C, G, and T). However, you want to catalogthem, 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 linecontains two integers: a positive integer n (0 < n <= 50) giving thelength of the strings; and a positive integer m (0 < m <= 100) giving thenumber of strings. These are followed by m lines, each containing a string oflength n.
Output
Output the list ofinput strings, arranged from ``most sorted'' to ``least sorted''. Since twostrings 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
这道题的题意是输入 N 个字符串,按照字符串的逆序数由最少到最大开始输出。解题思路是输入一个字符串后马上计算其逆序数,然后用一个数组 a[i] 存储逆序数,再把 a[i] 的值乘以 1000 ,再加上 i 。这样做的目的是排序的时候可以直接用 a[i] 进行快排,而不影响其对应的字符串(因为 m 最大为 100 ,乘以 1000 后排序可以直接得到排序结果,输出的时候按照 a[i]%1000 输出对应的数组序号)。即可得到结果。#include
#include
#include
char a[101][51];
int n[101];
//快排函数
int cmp(const void * a, const void * b)
{
return((*(int*)a-*(int*)b));
}
int main()
{
int i,r,c;
scanf("%d%d",&r,&c);
for(i=0;ia[i][m])
n[i]++;
n[i]=n[i]*1000+i; //逆序数x1000+原序号储存,快排时不影响原序号
}
qsort(n,c,sizeof(n[0]),cmp); //快排
for(i=0;i
Presentation Error:呈现错误(结果是正确的,但是多输出空格或回车)
参考:http://zhidao.baidu.com/question/86523631.html
ps:自己写的代码比较大而慢,特百度下学习好代码,上面代码出自:
http://wenku.baidu.com/view/a96624d226fff705cc170a42.html
成绩:
1 2367318 chenda91 4K 0MS Pascal 847B 2007-07-21 15:48:11
3 2390755(5) tianjie 8K 0MS C 426B 2007-07-26 04:15:47
3029 11336237(3) dragoo1 172K 0MS C++ 475B 2013-03-11 09:34:47