我正在尝试解决Python中的Longest Common Subsequence。我已经完成了它并且它工作正常,虽然我已经提交了它并且说它已经部分完成了50%。我不确定我在这里失踪了什么,感谢任何帮助。
挑战说明:
您将获得两个序列。编写程序以确定两个字符串之间最长的公共子序列(每个字符串的最大长度为50个字符)。注意:此子序列不需要是连续的。输入文件可能包含空行,需要忽略这些行。
INPUT SAMPLE:
第一个参数是一个文件名的路径,每个行包含两个字符串,以分号分隔。您可以假设每个测试用例只有一个唯一的子序列。 E.g:
XMJYAUZ;MZJAWXU
输出样本:
最长的共同子序列。确保您打印的每一行都没有尾随空格。 E.g:
MJAU
我的代码是
# LONGEST COMMON SUBSEQUENCE
import argparse
def get_longest_common_subsequence(strings):
# here we will store the subsequence list
subsequences_list = list()
# split the strings in 2 different variables and limit them to 50 characters
first = strings[0]
second = strings[1]
startpos = 0
# we need to start from each index in the first string so we can find the longest subsequence
# therefore we do a loop with the length of the first string, incrementing the start every time
for start in range(len(first)):
# here we will store the current subsequence
subsequence = ''
# store the index of the found character
idx = -1
# loop through all the characters in the first string, starting at the 'start' position
for i in first[start:50]:
# search for the current character in the second string
pos = second[0:50].find(i)
# if the character was found and is in the correct sequence add it to the subsequence and update the index
if pos > idx:
subsequence += i
idx = pos
# if we have a subsequence, add it to the subsequences list
if len(subsequence) > 0:
subsequences_list.append(subsequence)
# increment the start
startpos += 1
# sort the list of subsequences with the longest at the top
subsequences_list.sort(key=len, reverse=True)
# return the longest subsequence
return subsequences_list[0]
def main():
parser = argparse.ArgumentParser()
parser.add_argument('filename')
args = parser.parse_args()
# read file as the first argument
with open(args.filename) as f:
# loop through each line
for line in f:
# if the line is empty it means it's not valid. otherwise print the common subsequence
if line.strip() not in ['\n', '\r\n', '']:
strings = line.replace('\n', '').split(';')
if len(strings[0]) > 50 or len(strings[1]) > 50:
break
print get_longest_common_subsequence(strings)
return 0
if __name__ == '__main__':
main()
答案 0 :(得分:0)
以下解决方案从分号分隔的字符串对中打印无序/未排序的最长公共子序列/子字符串。如果该对中的字符串长度超过50个字符,则跳过该对(如果需要,则不难将其修剪为长度50)。
注意:如果需要排序/排序,可以实现(按字母顺序排序,或按第一个字符串的顺序排序或按第二个字符串的顺序排序。
with open('filename.txt') as f:
for line in f:
line = line.strip()
if line and ';' in line and len(line) <= 101:
a, b = line.split(';')
a = set(a.strip())
b = set(b.strip())
common = a & b # intersection
if common:
print ''.join(common)
另请注意:如果子字符串具有内部公共空格(即ABC DE; ZM YCA
),则它将成为输出的一部分,因为它不会被剥离。如果不需要,则可以将a = set(a.strip())
行替换为a = {char for char in a if char.strip()}
,同样替换为b
。