每天一道算法_8_DNA Sorting

DescriptionOne 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 mea

大家好,又见面了,我是全栈君。

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.
 

Sample Input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

 

Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

 

代码如下:

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.util.Arrays;
import java.util.Comparator;

//import java.util.Scanner;   
class DNA {
	String value;
	int level;
}

class DNAType implements Comparator<Object> {
	public int compare(Object arg0, Object arg1) {
		DNA obj1 = (DNA) arg0;
		DNA obj2 = (DNA) arg1;
		return obj1.level - obj2.level;
	}
}

public class DNASorting {
	public static void main(String[] args) throws Exception {
		int i;
		// Scanner cin = new Scanner(System.in);
		DataInputStream cin = new DataInputStream(new BufferedInputStream(
				System.in));
		// int col = cin.nextInt();
		// int row = cin.nextInt();
		// cin.nextLine();
		String s = new String();
		s = cin.readLine();
		String n[] = s.split(" ");
		int col = Integer.parseInt(n[0]);
		int row = Integer.parseInt(n[1]);
		DNA dna[] = new DNA[row];
		for (i = 0; i < row; ++i) {
			String line = new String();
			// line = cin.nextLine();
			line = cin.readLine();
			dna[i] = new DNA();
			dna[i].value = line;
			dna[i].level = getLevel(line);
		}
		DNAType comp = new DNAType();
		Arrays.sort(dna, comp);
		for (i = 0; i < row; ++i) {
			System.out.println(dna[i].value);
		}
	}

	private static int getLevel(String line) {
		int i, j, t = 0;
		for (i = 0; i < line.length(); ++i) {
			for (j = i + 1; j < line.length(); ++j) {
				if (line.charAt(i) > line.charAt(j)) {
					++t;
				}
			}
		}
		return t;
	}
}

 

作者:jason0539

微博:http://weibo.com/2553717707

博客:http://blog.csdn.net/jason0539(转载请说明出处)

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/121381.html原文链接:https://javaforall.net

(0)
上一篇 2022年3月10日 下午8:00
下一篇 2022年3月10日 下午9:00


相关推荐

  • 网页制作实验步骤_web简易开发

    网页制作实验步骤_web简易开发web实验2制作简单网页(HTML+CSS)一、实验目的1.掌握文本样式的设置。2.掌握图像样式的设置。3.掌握各种媒体的插入方法。二、实验内容采用DIV+CSS,制作“在线电影”页面。三、操作提示1.新建网站的文件夹,网站图像素材保存在images文件夹中,媒体文件放在flash文件中。2.新建index.html页面,要求: 页面字体大小为14px,文本颜色为#000; 页面背景颜色为#edb8d2; 上下左右距均为0。3.利用div布局,宽度为900px,居中对齐。

    2022年10月13日
    6
  • http://wwwnno00.irrlicht3d.cn:8011/forum-20-3.html「建议收藏」

    http://wwwnno00.irrlicht3d.cn:8011/forum-20-3.html「建议收藏」
    http://wwwnno00.irrlicht3d.cn:8011/forum-20-3.html

    2022年6月3日
    37
  • mybatis逆向工程是什么意思_长话短说的方法

    mybatis逆向工程是什么意思_长话短说的方法目录Mybatis逆向工程一、通过Eclipse插件完成Mybatis逆向工程1.在线安装Eclipse插件2.新建一个JavaProject项目3.编写配置文件4.使用插件运行二、通过Java代码完成Mybatis逆向工程1.新建一个JavaProject项目2.编写配置文件3.编写生成代码程序三、通过Maven完成Mybatis逆向工程1…

    2022年8月21日
    6
  • PTA乙级1034

    PTA乙级10341034 本题要求编写程序 计算 2 个有理数的和 差 积 商 输入格式 输入在一行中按照 a1 b1a2 b2 的格式给出两个分数形式的有理数 其中分子和分母全是整型范围内的整数 负号只可能出现在分子前 分母不为 0 输出格式 分别在 4 行中按照有理数 1 运算符有理数 2 结果的格式顺序输出 2 个有理数的和 差 积 商 注意输出的每个有理数必须是该有理数的最简形式 ka

    2026年3月17日
    2
  • XAMPP安装和配置(for mac)

    XAMPP安装和配置(for mac)XAMPP 安装和配置 formac XAMPP 安装和配置 formac 一 XAMPP 介绍二 XAMPP 安装三 XAMPP 配置 0 按 command 空格键输入 private etc 路径 找到 etc 目录中的 hosts 文件 把 hosts 文件拖到 sublime 编辑器中进行编辑 添加虚拟的域名 PS 我这里的域名添加了两个 www job com 和 api job

    2026年3月16日
    2
  • Prometheus monitor RabbitMQ

    Prometheus monitor RabbitMQ

    2021年7月3日
    142

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号