POJ1007
DNA Sorting
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 64552 | Accepted: 25498 |
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
题目类型:排序
题目大意:
一个序列的“反序数”是指这个序列中未规则地排序(从小大大排序)的项的数量和。例如:“DAABEC”,它的度便是5,因为D比排在它后面字母中的4个大,E比排在它后面的1个字母大,所以它的反序数为4+1=5.
现在给你 m 个长度为 n 的序列,按照各序列反序数从小到大顺序输出这些已排序数列。
解题思路:
一:先把这些序列输入,存放到二维字符型数组里;
二:计算各序列反序数,存放到一个整型数组中;(前两步可以合在一起,输入的同时进行处理)
三:按照反序数数组的大小排序;
四:按照已有顺序输出各序列。
由于对序列各个体都排序会比较难入手,比较容易想到的是把它的地址(对应的 i 值)存放到另一整型数组中,在反序数数组排序的同时进行排序。这样,按照反序数从小到大排列好的同时,与其所关联的序列一一对应。按照这个顺序,依次输出即可。
我的代码如下:
#include<stdio.h>
#include<string.h>
int main()
{
int n,m,i,j,k,sum,temp;
char str[105][55];
int a[100],b[100];
scanf("%d%d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%s",str[i]);
sum=0;
for(j=0;j<strlen(str[i])-1;j++)
{
for(k=j+1;k<strlen(str[i]);k++)
{
if(str[i][k]<str[i][j])
sum++;
}
}
a[i]=sum;
b[i]=i;
}
for(i=0;i<m-1;i++)
{
for(j=0;j<m-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];a[j]=a[j+1];a[j+1]=temp;
temp=b[j];b[j]=b[j+1];b[j+1]=temp;
}
}
}
for(i=0;i<m;i++)
printf("%s\n",str[b[i]]);
return 0;
}
这题确实还是很水。。。排序的过程中采用了模拟的方法(知识个人见解)。。。