首页 > 代码库 > POJ 1007:DNA排序

POJ 1007:DNA排序

AC CODE:

 1 import java.util.Scanner; 2  3 public class Main { 4  5     public static void main(String[] args) { 6         Scanner input = new Scanner(System.in); 7          8         // 得到数据 9         int n = input.nextInt();10         int m = input.nextInt();11         String temp = input.nextLine(); // 获得输入m后的换行符12         String[] dna = new String[m];13         for (int i = 0; i < m; i++) {14             dna[i] = input.nextLine();15         }16         17         // 获得每个DNA串中逆序对的数量18         int[] record = getInitInt(m);19         for (int i = 0; i < m; i++) {20             for (int j = 0; j < n; j++) {21                 for (int k = j + 1; k < n; k++) {22                     if (isBiggerChar(dna[i].charAt(j), dna[i].charAt(k))) {23                         record[i]++;24                     }25                 }26             }27         }28         29         // 得到这些DNA数列中最大的逆序对是多少30         int max = -1;31         for (int i = 0; i < m; i++) {32             if (record[i] > max) {33                 max = record[i];34             }35         }36         37         // 得到这些DNA串中逆序对从小到大的一个顺序队列mark[]38         int[] mark = new int[m];39         for (int i = 0, k = 0; i <= max; i++) {40             for (int j = 0; j < m; j++) {41                 if (record[j] == i) {42                     mark[k++] = j;43                 }44             }45         }46         47         // 打印48         for (int i = 0; i < m; i++) {49             System.out.println(dna[mark[i]]);50         }51         52     }53     54     private static int[] getInitInt(int m) {55         int[] record = new int[m];56         for (int i = 0; i < m; i++) {57             record[i] = 0;58         }59         return record;60     }61     62     private static boolean isBiggerChar(char a, char b) {63         if ((a - b) > 0) {64             return true;65         }else {66             return false;67         }68     }69 70 }