DNA序列(DNA Consensus String)

2023-05-16

DNA Consensus String

Time limit: 3.000 seconds

\epsfbox{p3602.eps}Figure 1.DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of four different nucleotides, namely Adenine, Thymine, Guanine, and Cytosine as shown in Figure 1. If we represent a nucleotide by its initial character, a DNA strand can be regarded as a long string (sequence of characters) consisting of the four characters A, T, G, and C. For example, assume we are given some part of a DNA strand which is composed of the following sequence of nucleotides:


``Thymine-Adenine-Adenine-Cytosine-Thymine-Guanine-Cytosine-Cytosine-Guanine-Adenine-Thymine"


Then we can represent the above DNA strand with the string ``TAACTGCCGAT." The biologist Prof. Ahn found that a gene X commonly exists in the DNA strands of five different kinds of animals, namely dogs, cats, horses, cows, and monkeys. He also discovered that the DNA sequences of the gene X from each animal were very alike. See Figure 2.


 DNA sequence of gene X
Cat:GCATATGGCTGTGCA
Dog:GCAAATGGCTGTGCA
Horse:GCTAATGGGTGTCCA
Cow:GCAAATGGCTGTGCA
Monkey:GCAAATCGGTGAGCA


Figure 2. DNA sequences of gene X in five animals.


Prof. Ahn thought that humans might also have the gene X and decided to search for the DNA sequence of X in human DNA. However, before searching, he should define a representative DNA sequence of gene X because its sequences are not exactly the same in the DNA of the five animals. He decided to use the Hamming distance to define the representative sequence. The Hamming distance is the number of different characters at each position from two strings of equal length. For example, assume we are given the two strings ``AGCAT" and ``GGAAT." The Hamming distance of these two strings is 2 because the 1st and the 3rd characters of the two strings are different. Using the Hamming distance, we can define a representative string for a set of multiple strings of equal length. Given a set of strings S = s1,..., sm of length n, the consensus error between a string y of length n and the set S is the sum of the Hamming distances between y and each si in S. If the consensus error between y and S is the minimum among all possible strings y of length n, y is called a consensus string of S. For example, given the three strings ``AGCAT" ``AGACT" and ``GGAAT" the consensus string of the given strings is ``AGAAT" because the sum of the Hamming distances between ``AGAAT" and the three strings is 3 which is minimal. (In this case, the consensus string is unique, but in general, there can be more than one consensus string.) We use the consensus string as a representative of the DNA sequence. For the example of Figure 2 above, a consensus string of gene X is ``GCAAATGGCTGTGCA" and the consensus error is 7.

Input 

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integers m and n which are separated by a single space. The integer m (4$ \le$m$ \le$50) represents the number of DNA sequences and n (4$ \le$n$ \le$1000) represents the length of the DNA sequences, respectively. In each of the next m lines, each DNA sequence is given.

Output 

Your program is to write to standard output. Print the consensus string in the first line of each case and the consensus error in the second line of each case. If there exists more than one consensus string, print the lexicographically smallest consensus string. The following shows sample input and output for three test cases.

Sample Input 


3 
5 8 
TATGATAC 
TAAGCTAC 
AAAGATCC 
TGAGATAC 
TAAGATGT 
4 10 
ACGTACGTAC 
CCGTACGTAG 
GCGTACGTAT 
TCGTACGTAA 
6 10 
ATGTTACCAT 
AAGTTACGAT 
AACAAAGCAA 
AAGTTACCTT 
AAGTTACCAA 
TACTTACCAA
  

Sample Output 


TAAGATAC 
7 
ACGTACGTAA 
6 
AAGTTACCAA 
12
  

这里要说明一下:

以下分析(包括C++代码)来源于:吊炸天的柯小帅
http://www.2cto.com/kf/201312/263670.html


题目大意:给定m个长度为n的DNA序列,求一个DNA序列,使其到所有序列的总hamming距离尽量小,如有多解,输出最小解。

解题思路:因为每个DNA序列都是相同长度的,所以可以枚举每一位DNA码,然后用贪心的思想选取出现次数最多的,如果有两个最多的,按要求用字典序小的。

以下是 吊炸天的柯小帅 的C++代码:

#include <stdio.h>
#include <string.h>
 
const int N = 1005;
const int M = 105;
 
int n, m, cnt[M];
char dna[M][N];
 
void init() {
    scanf(%d%d, &m, &n);
    for (int i = 0; i < m; i++) scanf(%s, dna[i]);
}
 
void solve() {
    int ans = 0;
    for (int i = 0; i < n; i++) {
        int Max = 0, id;
        memset(cnt, 0, sizeof(cnt));
        for (int j = 0; j < m; j++) {
            int tmp = dna[j][i] - 'A';
            cnt[tmp]++;
            if (cnt[tmp] > Max) {
                Max = cnt[tmp];
                id = tmp;
            } else if (cnt[tmp] == Max && tmp < id)
                id = tmp;
        }
        ans += m - Max;
        printf(%c, 'A' + id);
    }
    printf(
%d
, ans);
}
 
int main() {
    int cas;
    scanf(%d, &cas);
    while (cas--) {
        init();
        solve();
    }
    return 0;
}

而下面是本人原创的java代码:

import java.util.Scanner;

public class DNAConsensusString {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int T = input.nextInt();
		for(int i = 0; i < T; i++) {
			int m = input.nextInt();
			int n = input.nextInt();
			
			int[][] countArr = new int[4][n];//统计输入的DNA序列各列中 A、C、G、T的个数
			for(int j = 0; j < 4; j++)
				for(int k = 0; k < n; k++)
					countArr[j][k] = 0;
			
			for(int j = 0; j < m; j++) {
				String str = input.next();
				for(int k = 0; k < n; k++) {
					char c = str.charAt(k);
					if(c == 'A')
						countArr[0][k]++;
					else if(c == 'C')
						countArr[1][k]++;
					else if(c == 'G')
						countArr[2][k]++;
					else
						countArr[3][k]++;
				}
			}
			
			int hanmmingSum = 0;
			int max;
			char id = 'A';
			char[] temp = {'A', 'C', 'G', 'T'};
			for(int j = 0; j < n; j++) {		
				max = -1;
				//找到列中最大值,以及对应的字母(当最大值相同时,找出最小字符序的字符)
				for(int k = 0; k < 4; k++) {
					if(max < countArr[k][j]) {
						max = countArr[k][j];
						id = temp[k];
					}
					else if(max == countArr[k][j] && temp[k] < id)
						id = temp[k];
				}				
				hanmmingSum += (m - max);
				System.out.print(id);
			}
			System.out.println();
			System.out.println(hanmmingSum);
		}
	}
}




本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DNA序列(DNA Consensus String) 的相关文章

随机推荐

  • vscode通过文件名查找文件的方法

    vscode通过文件名查找文件的方法 这篇文章给大家分享的是有关vscode通过文件名查找文件的方法的内容 小编觉得挺实用的 xff0c 因此分享给大家做个参考 一起跟随小编过来看看吧 按快捷键ctrl 43 p可以弹出一个小窗 xff0c
  • HTML几种设置水平居中和垂直居中的方式

    水平居中 1 平居中设置 定宽块状元素 当被设置元素为 块状元素 时用 text align xff1a center 就不起作用了 xff0c 这时也分两种情况 xff1a 定宽块状元素和不定宽块状元素 这一小节我们先来讲一讲定宽块状元素
  • 【Django网络安全】如何正确设置跨域

    原文作者 xff1a 我辈李想 版权声明 xff1a 文章原创 xff0c 转载时请务必加上原文超链接 作者信息和本声明 Django网络安全 Django网络安全 如何正确设置跨域 Django网络安全 如何正确防护CSRF跨站点请求伪造
  • 我怎么能只懂得使用Redis呢?(一)

    前言 最近在回顾 amp 学习Redis相关的知识 xff0c 也是本着分享的态度和知识点的记录在这里写下相关的文章 希望能帮助各位读者学习或者回顾到Redis相关的知识 xff0c 当然 xff0c 本人才疏学浅 xff0c 在学习和写作
  • 统计回文子串

    1054 统计回文子串 分数 2 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 字符串处理回文串 题目描述 现在给你一个字符串S xff0c 请你计算S中有多少连续子串是回文串 输入格式 输入
  • 小明养猪的故事

    1064 小明养猪的故事 分数 1 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 递推 题目描述 话说现在猪肉价格这么贵 xff0c 小明也开始了养猪生活 说来也奇怪 xff0c 他养的猪一出
  • 找新朋友

    1079 找新朋友 分数 1 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 简单数学题公约数 题目描述 新年快到了 xff0c 天勤准备搞一个聚会 xff0c 已经知道现有会员N人 xff0c
  • 生物节律

    1084 生物节律 分数 2 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 数学题 题目描述 有些人相信 xff0c 人从出生开始就有三个生物周期 这三个生物周期分别是体力 xff0c 情绪和
  • 鸡兔同笼

    鸡兔同笼 已知鸡和兔的总数量为n xff0c 总腿数为m 输入n和m xff0c 依次输出鸡的数目和兔的数目 如果无解 xff0c 则输出No answer 样例输入 xff1a 14 32 样例输出 xff1a 12 2 样例输入 xff
  • 赌徒

    1097 赌徒 分数 2 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 查找二分查找 题目描述 有n个赌徒打算赌一局 规则是 xff1a 每人下一个赌注 xff0c 赌注为非负整数 xff0c
  • 排列(permutation)

    排列 xff08 permutation xff09 用1 2 3 xff0c xff0c 9组成3个三位数 abc xff0c def 和 ghi xff0c 每个数字恰好使用一次 xff0c 要求 abc def ghi 61 1 2
  • SQL Server Management Studio 使用方法手记

    本方法只记录自己的操作方法 xff0c 仅供参考 一 下载安装 SQL Server Management Studio V15 0 18424 0 xff0c 链接 xff1a https download csdn net downlo
  • 蛇形填数(矩阵)

    蛇形填数 在 n x n 方针里填入 1 2 xff0c xff0c n x n xff0c 要求填成蛇形 例如 xff1a n 61 4时方阵为 xff1a 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 上
  • WERTYU

    Problem 1343 WERTYU Time Limit 1000 mSec Memory Limit 32768 KB Problem Description A common typing error is to place the
  • 分子量(Molar Mass)

    Molar mass Time limit 3 000 seconds An organic compound is any member of a large class of chemical compounds whose molec
  • 数数字(Digit Counting)

    Digit Counting Time limit 3 000 seconds Trung is bored with his mathematics homeworks He takes a piece of chalk and star
  • 周期串(Periodic Strings)

    周期串 Periodic Strings 如果一个字符串可以由某个长度为k的字符串重复多次得到 xff0c 则称该串以k为周期 例如 xff0c abcabcabcabc以3为周期 xff08 注意 xff0c 它也以6和12为周期 xff
  • 谜题(Puzzle)

    Puzzle Time limit 3 000 seconds Puzzle A children 39 s puzzle that was popular 30 years ago consisted of a 5x5 framewhic
  • 纵横字谜的答案(Crossword Answers)

    Crossword Answers Time limit 3 000 seconds Crossword Answers A crossword puzzle consists of a rectangular grid of black
  • DNA序列(DNA Consensus String)

    DNA Consensus String Time limit 3 000 seconds Figure 1 DNA Deoxyribonucleic Acid is the molecule which contains the gene