longest-substring

Longest Common Substring without cutting a word- python

拟墨画扇 提交于 2019-11-28 05:34:09
问题 Given the following, i can find the longest common substring: s1 = "this is a foo bar sentence ." s2 = "what the foo bar blah blah black sheep is doing ?" def longest_common_substring(s1, s2): m = [[0] * (1 + len(s2)) for i in xrange(1 + len(s1))] longest, x_longest = 0, 0 for x in xrange(1, 1 + len(s1)): for y in xrange(1, 1 + len(s2)): if s1[x - 1] == s2[y - 1]: m[x][y] = m[x - 1][y - 1] + 1 if m[x][y] > longest: longest = m[x][y] x_longest = x else: m[x][y] = 0 return s1[x_longest -

Python: Length of longest common subsequence of lists

谁说我不能喝 提交于 2019-11-27 20:55:39
Is there a built-in function in python which returns a length of longest common subsequence of two lists? a=[1,2,6,5,4,8] b=[2,1,6,5,4,4] print a.llcs(b) >>> 3 I tried to find longest common subsequence and then get length of it but I think there must be a better solution. You can easily retool a LCS into a LLCS: def lcs_length(a, b): table = [[0] * (len(b) + 1) for _ in xrange(len(a) + 1)] for i, ca in enumerate(a, 1): for j, cb in enumerate(b, 1): table[i][j] = ( table[i - 1][j - 1] + 1 if ca == cb else max(table[i][j - 1], table[i - 1][j])) return table[-1][-1] Demo: >>> a=[1,2,6,5,4,8] >>>

Longest Common Substring

一个人想着一个人 提交于 2019-11-27 08:28:38
问题 We have two strings a and b respectively. The length of a is greater than or equal to b . We have to find out the longest common substring. If there are multiple answers then we have to output the substring which comes earlier in b (earlier as in whose starting index comes first). Note: The length of a and b can be up to 10 6 . I tried to find the longest common substring using suffix array (sorting the suffixes using quicksort). For the case when there is more than one answer, I tried

Python: Length of longest common subsequence of lists

孤者浪人 提交于 2019-11-27 04:28:54
问题 Is there a built-in function in python which returns a length of longest common subsequence of two lists? a=[1,2,6,5,4,8] b=[2,1,6,5,4,4] print a.llcs(b) >>> 3 I tried to find longest common subsequence and then get length of it but I think there must be a better solution. 回答1: You can easily retool a LCS into a LLCS: def lcs_length(a, b): table = [[0] * (len(b) + 1) for _ in xrange(len(a) + 1)] for i, ca in enumerate(a, 1): for j, cb in enumerate(b, 1): table[i][j] = ( table[i - 1][j - 1] +

Longest common substring from more than two strings - Python

谁说胖子不能爱 提交于 2019-11-26 05:23:13
问题 I\'m looking for a Python library for finding the longest common sub-string from a set of strings . There are two ways to solve this problem : using suffix trees using dynamic programming. Method implemented is not important. It is important it can be used for a set of strings (not only two strings). 回答1: These paired functions will find the longest common string in any arbitrary array of strings: def long_substr(data): substr = '' if len(data) > 1 and len(data[0]) > 0: for i in range(len