UVA1368 DNA Consensus String

2023-12-02 17:20
文章标签 dna string consensus uva1368

本文主要是介绍UVA1368 DNA Consensus String,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

DNA Consensus String

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 1 above, a consensus string of gene X is “GCAAATGGCTGTGCA” and the consensus error is 7.

image-20231202140726494

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 ≤ m ≤ 50) represents the number of DNA sequences and n(4 ≤ n ≤ 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.

DNA序列 DNA Consensus String

题面翻译

输入 m m m个长度均为 n n n DNA \text{DNA} DNA序列,求一个 DNA \text{DNA} DNA序列,到所有序列的总 Hamming \text{Hamming} Hamming距离尽量小。两个等长字符串的 Hamming \text{Hamming} Hamming距离等于字符不同的位置个数,如 ACGT \text{ACGT} ACGT GCGA \text{GCGA} GCGA Hamming \text{Hamming} Hamming距离为 2 2 2(左数第 1 1 1 4 4 4个字符不同)。

输入整数 m m m n n n 4 ≤ m ≤ 50 4\leq m \leq 50 4m50 4 ≤ n ≤ 1000 4\leq n \leq 1000 4n1000),以及 m m m个长度为 n n n DNA \text{DNA} DNA序列,(只包含字母 A A A C C C G G G T T T),输出到 m m m个序列的 H a m m i n g Hamming Hamming距离和最小的 DNA \text{DNA} DNA序列和对应的距离。如有多解,要求字典序最小的解。

题目描述

PDF

输入格式

输出格式

Solution

采用贪心策略

str[i][j]为第i个字符串的第j个元素,因为字符串序列是等长的首先比较相同位置的字符,并记录其A、G、C、T的个数,当所有m个字符串的第i个位置遍历完毕,计算A、G、C、T个数最大,当数目相同时使用字典序最小的的并作为std_s[i]

//
// Created by Gowi on 2023/12/2.
//#include <iostream>
#include <string>#define N 55using namespace std;int main() {string str[N];int T;cin >> T;while (T--) {int m, n;string std_s;cin >> m >> n;for (int i = 0; i < m; ++i) {cin >> str[i];}int sum = 0;for (int i = 0; i < n; ++i) {int a = 0, c = 0, g = 0, t = 0, index_n = 0;char index_c = 'Z';for (int j = 0; j < m; ++j) {if (str[j][i] == 'A') {a++;} else if (str[j][i] == 'C') {c++;} else if (str[j][i] == 'G') {g++;} else {t++;}index_n = max(a, max(c, max(g, t)));}if (t == index_n && 'T' < index_c) {index_c = 'T';}if (g == index_n && 'G' < index_c) {index_c = 'G';}if (c == index_n && 'C' < index_c) {index_c = 'C';}if (a == index_n && 'A' < index_c) {index_c = 'A';}std_s += index_c;if (index_c != 'A') {sum += a;}if (index_c != 'G') {sum += g;}if (index_c != 'C') {sum += c;}if (index_c != 'T') {sum += t;}}std_s[n] = '\0';cout << std_s << endl;cout << sum << endl;}
}

这篇关于UVA1368 DNA Consensus String的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/446174

相关文章

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

redis数据结构之String详解

《redis数据结构之String详解》Redis以String为基础类型,因C字符串效率低、非二进制安全等问题,采用SDS动态字符串实现高效存储,通过RedisObject封装,支持多种编码方式(如... 目录一、为什么Redis选String作为基础类型?二、SDS底层数据结构三、RedisObject

Java获取当前时间String类型和Date类型方式

《Java获取当前时间String类型和Date类型方式》:本文主要介绍Java获取当前时间String类型和Date类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Java获取当前时间String和Date类型String类型和Date类型输出结果总结Java获取

java String.join()方法实例详解

《javaString.join()方法实例详解》String.join()是Java提供的一个实用方法,用于将多个字符串按照指定的分隔符连接成一个字符串,这一方法是Java8中引入的,极大地简化了... 目录bVARxMJava String.join() 方法详解1. 方法定义2. 基本用法2.1 拼接

Java字符串处理全解析(String、StringBuilder与StringBuffer)

《Java字符串处理全解析(String、StringBuilder与StringBuffer)》:本文主要介绍Java字符串处理全解析(String、StringBuilder与StringBu... 目录Java字符串处理全解析:String、StringBuilder与StringBuffer一、St

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

如何解决mysql出现Incorrect string value for column ‘表项‘ at row 1错误问题

《如何解决mysql出现Incorrectstringvalueforcolumn‘表项‘atrow1错误问题》:本文主要介绍如何解决mysql出现Incorrectstringv... 目录mysql出现Incorrect string value for column ‘表项‘ at row 1错误报错

java String.join()的使用小结

《javaString.join()的使用小结》String.join()是Java8引入的一个实用方法,用于将多个字符串按照指定分隔符连接成一个字符串,本文主要介绍了javaString.join... 目录1. 方法定义2. 基本用法2.1 拼接多个字符串2.2 拼接集合中的字符串3. 使用场景和示例3