随笔 - 145  文章 - 0  评论 - 6  阅读 - 18万

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 }
复制代码

 

posted on   wastonl  阅读(189)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示