poj 1007 DNA Sorting

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 46242 Accepted: 18064

  本题比较简单,可以采用结构体进行快排,结构体中可以有 data用来记录  inversions,有str 记录字符串,还可以有一个num记录输入进去的次序(我听说快排是不稳定排序,所以还是加上一个num进行二级排序比较保险,否则如果出现data相同的情况,难保不会出现次序的问题)

    代码:

  

  
    
1 #include < stdio.h >
2 #include < stdlib.h >
3   struct node
4 {
5 int data;
6 int num;
7 char str[ 60 ];
8 }s[ 105 ];
9 int cmp( const void * a, const void * b)
10 {
11 struct node * c = (node * )a;
12 struct node * d = (node * )b;
13 if (c -> data != d -> data)
14 return c -> data - d -> data;
15 else
16 return c -> num - d -> num;
17 };
18 int main()
19 {
20 int n,m,i,j,count,k;
21 while (scanf( " %d%d " , & n, & m) != EOF)
22 {
23 for (i = 1 ;i <= m;i ++ )
24 {
25 scanf( " %s " ,s[i].str);
26 getchar();
27 count = 0 ;
28 for (j = 0 ;j < n;j ++ )
29 {
30 for (k = j + 1 ;k < n;k ++ )
31 if (s[i].str[j] > s[i].str[k])
32 count ++ ;
33 }
34 s[i].data = count;
35 s[i].num = i;
36 }
37 qsort(s + 1 ,m, sizeof (s[ 0 ]),cmp);
38 for (i = 1 ;i <= m;i ++ )
39 printf( " %s\n " ,s[i].str);
40 }
41 return 0 ;
42 }
43

 

你可能感兴趣的:(sort)