zoukankan      html  css  js  c++  java
  • Poj1007_DNA Sorting(面向对象方法)

    一、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.

    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.

    二、问题分析

          题意:求输入的一串字母的秩(字母序的规整程度),并按秩由小到大输出字符串。此题的难点不在于求秩,而是再求的秩之后怎么把它和字符串管连起来。由于求得秩之后还要把秩按升序排序,所以关联字符串就比较麻烦了。

    三、问题解决

    1. 一开始看到要关联两个值就想到了Map,于是就把秩的值存为键,把字符串存为值。发现要排序,于是就用了TreeMap来自动维护键的顺序。一切进展顺利,以为又轻松解掉一题。刚开始运行给定测试数据的时候,没有问题过了。但是一提交代码,悲剧啊,WA.为什么呢?于是又找了几组测试数据,发现有的测试数据的输出比输入的行数要少。几经测试,发现了一个很容易被忽视的问题,那就是几个字符串的秩很可能是相同的,而我们亲爱的Map的键必须是独一无二的,重复添加相同的键的键值对,Map是不会接受的。好的我刚开始的尝试就这样失败了。

    2. 后来一想,既然键必须唯一,那我就把原来的键值对交换一下了,把秩存入值,用字符串来做键。但又有一个问题,就是如果存在相同的秩,在查找键的过程中,会找到多个键。好像还是不行,但是还是可行的。在遇到有相同的键的情况下,每找到对应值的键后,就把键值对移除,那么在下一个相同秩进行查找是就不会出现上次查找的键了。这样做的过程比较麻烦,还要把秩从Map中分离出来,然后进行排序。之后又要把秩和Map中的值进行关联判断,再输出结果。虽然程序可以运行,并输出结果,但是提交上去发现Runtime error.

    3. 参考了大神的方法,用Java的面向对象的方法可以巧妙解决这个关联的问题。把每个字符串看做是一个对象,把秩看出是它的一个属性。而计算秩就放到类中进行。这样就把字符串和秩关联起来了。再用容器把每个DNA对象添加。之后调用Collections.sort(Collection,new compartor);来排序。这里用到了实现了Compartor接口的类,它是强行对某个对象 collection 进行整体排序 的比较函数。可以将 Comparator 传递给 sort 方法(如 或 ),从而允许在排序顺序上实现精确控制。排序之后,只要打印容器即可。应用语言特性来解决这个问题,小弟对您的敬仰如滔滔江水,绵绵不绝;又如黄河之水,一发不可收拾。

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Scanner;
    
    class DNA{
    	private int sortNum = 0;
    	public String str = null;
    	
    	public DNA(String str){
    		this.str=str;
    		int num = 0;
    		char[] c=str.toCharArray();
    		for(int i=0 ;i < c.length - 1 ;i++){
    			for(int j=i+1 ;j < c.length ;j++){
    				if(c[i] > c[j])
    					num++;
    			}
    		}
    		sortNum=num;
    	}
    	
    	public int getSortNum(){
    		return sortNum;
    	}	
    	
    	public String toString(){
    		return str;
    	}
    }
    
    class  DNACompartor implements Comparator{
    
    	public int compare(Object o1, Object o2) {
    		DNA a=(DNA) o1;
    		DNA b=(DNA) o2;
    		if(a.getSortNum() > b.getSortNum())
    			return 1;
    		else if(a.getSortNum() == b.getSortNum())
    			return 0;
    		else
    			return -1;
    	}
    	
    }
    public class N1007_DNASorting_Version2{
    	
        public static void main(String args[]){
        	Scanner cin =new Scanner(System.in);
        	int n =cin.nextInt();
        	int t =cin.nextInt();
        	ArrayList<DNA> list=new ArrayList<DNA>();
        	for(int i=0 ;i < t; i++){
        		DNA a=new DNA(cin.next());
        		list.add(a);
        	}
        	Collections.sort(list, new DNACompartor());
        	
            for(DNA s:list){
            	System.out.println(s.toString());
            }
        }
    }
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Java JMX 监管
    Spring Boot REST(一)核心接口
    JSR 规范目录
    【平衡树】宠物收养所 HNOI 2004
    【树型DP】叶子的颜色 OUROJ 1698
    【匈牙利匹配】无题II HDU2236
    【贪心】Communication System POJ 1018
    【贪心】Moving Tables POJ 1083
    Calling Extraterrestrial Intelligence Again POJ 1411
    【贪心】Allowance POJ 3040
  • 原文地址:https://www.cnblogs.com/AndyDai/p/4734217.html
Copyright © 2011-2022 走看看