poj 1007
DNA Sorting
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 73258 | Accepted: 29242 |
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
解题报告:
该题属于常规题,首先读取所有输入存入数组中,按照度量指标计算出sort的值,然后利用快速排序算法按照sort值排序.
注意点:
1.读入一个字符串用scanf("%s", a);避免循环读取每一个字符,如scanf("%c", a[i]);
2.快排的partition中,注意第43行和46行,是++l,不是l++;
代码如下:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct dna{ 5 char dna_str[51]; 6 int sort_count; 7 }dna_t; 8 9 dna_t *dna[100]; 10 int m, n; 11 12 void count(dna_t *node){ 13 int c = 0; 14 for(int i = 0; i < n; ++i){ 15 for(int j = i + 1; j < n; ++j){ 16 if(node->dna_str[i] > node->dna_str[j]) 17 ++c; 18 } 19 } 20 node->sort_count = c; 21 } 22 23 void input(){ 24 scanf("%d%d", &n, &m); 25 for(int i = 0; i < m; ++i){ 26 dna[i] = (dna_t*)malloc(sizeof(dna_t)); 27 scanf("%s", &dna[i]->dna_str); 28 count(dna[i]); 29 } 30 } 31 32 void swap(dna_t **a, dna_t **b){ 33 dna_t *temp = *a; 34 *a = *b; 35 *b = temp; 36 } 37 38 int partition(int p, int q){ 39 int s = dna[q]->sort_count; 40 int l = p - 1; 41 for(int i = p; i < q; ++i){ 42 if(dna[i]->sort_count <= s){ 43 swap(&dna[i], &dna[++l]); 44 } 45 } 46 swap(&dna[q], &dna[++l]); 47 return l; 48 } 49 50 void sort(int p, int q){ 51 if(p < q){ 52 int m = partition(p, q); 53 sort(p, m - 1); 54 sort(m + 1, q); 55 } 56 } 57 58 void output(){ 59 for(int i = 0; i < m; ++i){ 60 printf("%s\n", dna[i]->dna_str); 61 } 62 } 63 64 int main(int argc, char const *argv[]) 65 { 66 input(); 67 sort(0, m - 1); 68 output(); 69 return 0; 70 }