C语言pojDNA字符串问题

来源:互联网 发布:霍华德生涯得分数据 编辑:程序博客网 时间:2024/06/05 16:11
DNA Sorting
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 104426 Accepted: 41805

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 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT

Sample Output

CCCGGGGGGAAACATGAAGGGATCAGATTTATCGATGCATTTTTGGCCAATTTGGCCAAA
#include<stdio.h>#include<string.h>char s[101][51];int a[101];int n,m;void sort();int main(){int i,j,k;scanf("%d%d",&n,&m);for(i=0;i<m;i++){char c=getchar();for(j=0;j<n;j++){scanf("%c",&s[i][j]);}}sort();int flag=1;i=0;int l=0;L:for(i=0;i<m;i++){for(j=0;j<m;j++){if(a[j]!=10000000)l=1;}if(a[i]==10000000)continue;flag=1;if(i==m-1){printf("%s\n",s[i]); a[i]=10000000;goto L;    }for(j=i+1;j<m;j++){if(a[j]==10000000)continue;if(a[i]>a[j]){flag=0;break;    }if(flag==1){for(k=0;k<n;k++){printf("%c",s[i][k]);}printf("\n");a[i]=10000000;goto L;}if(l==0)break; }}void sort(){int i,j,k,flag=1;for(i=0;i<m;i++){int num=0;for(j=0;j<n-1;j++){for(k=j+1;k<n;k++){if(s[i][j]>s[i][k]){num++;}}}a[i]=num;}}