您的位置:首页 > 其它

DNA Sorting

2008-02-15 19:19 190 查看
FJNU.1900
PKU.1007

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.
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

My Program


#include<iostream>


#include<string.h>


#define N 500


#define M 1000


using namespace std;




int main()




...{


char str[M]
,temp
;


int d[M],t,p;


int m,n,i,j,k;


cin>>t;


for(p=0;p<t;p++)




...{


cin>>m>>n;


for(i=0;i<n;i++)




...{


cin>>str[i];


d[i]=0;


for(j=0;j<m;j++)


for(k=j+1;k<m;k++)


if(str[i][j]>str[i][k])


d[i]++;


}


for(i=0;i<n;i++)


for(j=i+1;j<n;j++)


if(d[i]>d[j])




...{


k=d[i];


d[i]=d[j];


d[j]=k;


strcpy(temp,str[i]);


strcpy(str[i],str[j]);


strcpy(str[j],temp);


}


for(i=0;i<n;i++)


cout<<str[i]<<endl;


cout<<endl;


}


return 0;


}

YOYO's Note:
很简单的字符串比较,
当时STL不知道可以写比较函数,
于是自己写了超烂的排序……
额,重点是,它说0 < n <= 50,1 < m <= 100,
但是实际上N开50、M开100是不够的,于是我就在后面加了个0……
还有就是 FJNU里的INPUT比PKU的要多一个输入测试例数,
这里贴的是FJNU里的代码和题目。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: