题解 | #DNA序列#

DNA序列

http://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a

两种方式:
一种复杂度为o(n^2):

while True:
    try:
        a, b = input(), int(input())
        maxStr, maxCnt = a[:b], a[:b].count("C") + a[:b].count("G")
        for i in range(0, len(a) - b):
            if a[i:i + b].count("C") + a[i:i + b].count("G") > maxCnt:
                maxCnt = a[i:i + b].count("C") + a[i:i + b].count("G")
                maxStr = a[i:i + b]
        print(maxStr)
    except:
        break

另一种:

while True:
    try:
        a,b,list1=list(input()),int(input()),[]
        c=len(a)-b+1
        for i in range(c):
            res=a[i:i+b]
            Count_G=res.count('G')
            Count_C=res.count('C')
            GC_ratio=(Count_G+Count_C)/b
            list1.append(GC_ratio)
        d=list1.index(max(list1))
        print(''.join(a[d:d+b]))
    except:
        break
全部评论
已经搞懂了, 应该 +1
点赞 回复 分享
发布于 2021-10-23 12:36
为什么第二种中: c = len(a) -b 然后还要加1. 第二个例子中也是暴力穷举 O(n^2) 吧
点赞 回复 分享
发布于 2021-10-23 12:32

相关推荐

牛客24669217...:都说老一辈喜欢花钱找关系找门路,其实都是一批人,和花几万找张雪峰填高考志愿的一类人
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务