poj 107 DNA sorting

关于Java的题解,也许效率低下,但是能解决不只是ACGT的序列字符串

代码如下:

 1 import java.util.*;
 2 public class Main {
 3     public static void main(String[] args){
 4         Scanner sc = new Scanner(System.in);
 5         TreeMap<Integer,String> map = new TreeMap<Integer,String>();
 6         //读入题目所给的信息
 7         int n = sc.nextInt(),m = sc.nextInt(),count;
 8         String[] strs = new String[m];
 9         for(int i=0;i<strs.length;i++){
10             strs[i] = sc.next();
11             if(strs[i].length()!=n)
12                 throw new RuntimeException();
13         }
14         //计算每一个序列的measure数,用count表示
15         for(int i=0;i<strs.length;i++){
16             count=0;
17             for(int j=0;j<strs[i].length()-1;j++){
18                 for(int k=j+1;k<strs[i].length();k++){
19                     char ch1 = strs[i].charAt(j);
20                     char ch2 = strs[i].charAt(k);
21                     if(ch1>ch2)
22                         count++;
23                 }
24             }
25             //如果发现序列中有相同的count,那么把该序列加到原序列的末尾,用来最后的输出
26             if(map.containsKey(count)){
27                 String value = map.get(count);
28                 map.put(count,value+strs[i]);
29             }
30             else
31                 map.put(count, strs[i]);
32         }
33         //输出答案序列
34         for(Map.Entry<Integer, String> entry : map.entrySet()){
35             String value = entry.getValue();
36             int size = value.length()/n;
37             if(size>1){
38                 int offset = 0;
39                 for(int i=1;i<=size;i++){
40                     System.out.println(value.substring(offset,offset+n));
41                     offset+=n;
42                 }
43             }
44             else
45                 System.out.println(value);
46         }
47     }    
48 }
原文地址:https://www.cnblogs.com/wt20/p/5528870.html