rosalind练习题十四

该程序旨在从一组不超过100个长度不超过1kb的DNA字符串中找出最长公共子串,也称为motif。使用动态规划的方法,从最短的字符串开始,逐个检查可能的子串,直到找到所有字符串共有的最长子串。在给定的示例数据中,输入包括三个DNA序列,输出的最长公共子串是AC。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# Problem

# A common substring of a collection of strings is a substring of every member of the collection. We say that a common substring is a longest common substring if there does not exist a longer common substring. For example, "CG" is a common substring of "ACGTACGT" and "AACCGTATA", but it is not as long as possible; in this case, "CGTA" is a longest common substring of "ACGTACGT" and "AACCGTATA".

# Note that the longest common substring is not necessarily unique; for a simple example, "AA" and "CC" are both longest common substrings of "AACC" and "CCAA".

#

# Given: A collection of k (k≤100) DNA strings of length at most 1 kbp each in FASTA format.

# Return: A longest common substring of the collection. (If multiple solutions exist, you may return any single solution.)

#

# Sample Dataset

# >Rosalind_1

# GATTACA

# >Rosalind_2

# TAGACCA

# >Rosalind_3

# ATACA

# Sample Output

# AC

# 题目要求我们找到一个 DNA 字符串集合中的最长公共子串,即motif。

def find_common_motif(strings):

    shortest_string = min(strings, key=len)

    for length in range(len(shortest_string), 0, -1):

        for start in range(len(shortest_string) - length + 1):

            substring = shortest_string[start:start + length]

            if all(substring in string for string in strings):

                return substring

    return ""

# 读入数据

strings = []

with open("input4.txt", "r") as f:

    current_string = ""

    for line in f:

        if line.startswith(">"):  # FASTA格式下一行为数据描述,跳过

            continue

        current_string += line.strip()

        if current_string[-1] == "":  # 处理最后一个字符为空格的情况

            current_string = current_string[:-1]

        strings.append(current_string)

        current_string = ""

# 寻找最长公共子串并打印输出

result = find_common_motif(strings)

print(result)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值