mydjm

 

poj1007

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.

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
 
 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 int cmp(const void *a,const void *b)
5 {
6 return *(int*)a-*(int*)b;
7 }
8 int main()
9 {
10 int i,j,k,m,n,a[100]={0};
11 char c[100][51];
12 scanf("%d %d",&n,&m);
13 for(i=0;i<m;i++)
14 {
15 scanf("%s",c[i]);
16 for(j=0;j<n;j++)
17 for(k=j+1;k<n;k++)
18 if(c[i][j]>c[i][k])
19 a[i]++;
20 a[i]=a[i]*1000+i;
21 }
22 qsort(a,m,sizeof(int),cmp);
23 for(i=0;i<m;i++)
24 printf("%s\n",c[a[i]%1000]);
25 return 0;
26 }

 

 

总结下:

我对排序不熟 于是直接上网搜 在百度文库找到一篇挺有意思的 于是看完之后自己模仿他的思路写~

提交的时候说我output limit exceeded。。我搞不懂怎么回事。。百度了半天 有人说 把才c[100][50]

改成c[100][51]试试 果然可以。。这是怎么回事啊。。。

这代码的思路还是挺有意思,把i跟数组c联系在了一起~~而且 最大的发现是 原来库函数里面有快排。。难怪上次

在百度看到有人自己写排序算法的时候有人感到惊讶~~呵呵 挺好用~~

 


posted on 2011-11-28 13:06  mydjm  阅读(147)  评论(0编辑  收藏  举报

导航