来自两个以上字符串的最长公共子字符串 - Python

时间:2010-05-23 18:37:40

标签: python string longest-substring

我正在寻找一个Python库,用于从一组字符串中查找最长的公共子字符串。有两种方法可以解决这个问题:

  • 使用后缀树
  • 使用动态编程。

实施的方法并不重要。重要的是它可以用于一组字符串(不仅仅是两个字符串)。

9 个答案:

答案 0 :(得分:52)

这些配对函数将在任意字符串数组中找到最长的公共字符串:

def long_substr(data):
    substr = ''
    if len(data) > 1 and len(data[0]) > 0:
        for i in range(len(data[0])):
            for j in range(len(data[0])-i+1):
                if j > len(substr) and is_substr(data[0][i:i+j], data):
                    substr = data[0][i:i+j]
    return substr

def is_substr(find, data):
    if len(data) < 1 and len(find) < 1:
        return False
    for i in range(len(data)):
        if find not in data[i]:
            return False
    return True


print long_substr(['Oh, hello, my friend.',
                   'I prefer Jelly Belly beans.',
                   'When hell freezes over!'])

毫无疑问,算法可以得到改进,而且我没有太多接触过Python,所以也许它在语法上也会更有效率,但它应该能够胜任。

编辑:列出第二个is_substr函数,如J.F.Sebastian所示。用法保持不变。注意:算法没有变化。

def long_substr(data):
    substr = ''
    if len(data) > 1 and len(data[0]) > 0:
        for i in range(len(data[0])):
            for j in range(len(data[0])-i+1):
                if j > len(substr) and all(data[0][i:i+j] in x for x in data):
                    substr = data[0][i:i+j]
    return substr

希望这有帮助,

杰森。

答案 1 :(得分:4)

我更喜欢is_substr,因为我发现它更具可读性和直观性:

def is_substr(find, data):
  """
  inputs a substring to find, returns True only 
  if found for each data in data list
  """

  if len(find) < 1 or len(data) < 1:
    return False # expected input DNE

  is_found = True # and-ing to False anywhere in data will return False
  for i in data:
    print "Looking for substring %s in %s..." % (find, i)
    is_found = is_found and find in i
  return is_found

答案 2 :(得分:3)

这可以缩短:

def long_substr(data):
  substrs = lambda x: {x[i:i+j] for i in range(len(x)) for j in range(len(x) - i + 1)}
  s = substrs(data[0])
  for val in data[1:]:
    s.intersection_update(substrs(val))
  return max(s, key=len)

set(可能)实现为哈希映射,这使得效率有点低。如果你(1)实现一个set数据类型作为trie和(2)只是将后缀存储在trie中然后强制每个节点成为一个端点(这相当于添加所有子串),那么理论上我会猜测这个宝宝的记忆效率非常高,特别是因为尝试的交叉非常容易。

然而,这很短暂,过早的优化是大量浪费时间的根源。

答案 3 :(得分:2)

def common_prefix(strings):
    """ Find the longest string that is a prefix of all the strings.
    """
    if not strings:
        return ''
    prefix = strings[0]
    for s in strings:
        if len(s) < len(prefix):
            prefix = prefix[:len(s)]
        if not prefix:
            return ''
        for i in range(len(prefix)):
            if prefix[i] != s[i]:
                prefix = prefix[:i]
                break
    return prefix

来自http://bitbucket.org/ned/cog/src/tip/cogapp/whiteutils.py

答案 4 :(得分:2)

# this does not increase asymptotical complexity
# but can still waste more time than it saves. TODO: profile
def shortest_of(strings):
    return min(strings, key=len)

def long_substr(strings):
    substr = ""
    if not strings:
        return substr
    reference = shortest_of(strings) #strings[0]
    length = len(reference)
    #find a suitable slice i:j
    for i in xrange(length):
        #only consider strings long at least len(substr) + 1
        for j in xrange(i + len(substr) + 1, length + 1):
            candidate = reference[i:j]  # ↓ is the slice recalculated every time?
            if all(candidate in text for text in strings):
                substr = candidate
    return substr

免责声明这对jtjacques的回答几乎没有增加。但是,希望这应该更易读更快它不适合评论,因此我在答案中发布这个。说实话,我对shortest_of不满意。

答案 5 :(得分:1)

您可以使用SuffixTree模块,该模块是基于广义后缀树的ANSI C实现的包装器。该模块易于处理....

看看:here

答案 6 :(得分:1)

如果有人正在寻找也可以获取任意对象序列列表的通用版本:

def get_longest_common_subseq(data):
    substr = []
    if len(data) > 1 and len(data[0]) > 0:
        for i in range(len(data[0])):
            for j in range(len(data[0])-i+1):
                if j > len(substr) and is_subseq_of_any(data[0][i:i+j], data):
                    substr = data[0][i:i+j]
    return substr

def is_subseq_of_any(find, data):
    if len(data) < 1 and len(find) < 1:
        return False
    for i in range(len(data)):
        if not is_subseq(find, data[i]):
            return False
    return True

# Will also return True if possible_subseq == seq.
def is_subseq(possible_subseq, seq):
    if len(possible_subseq) > len(seq):
        return False
    def get_length_n_slices(n):
        for i in xrange(len(seq) + 1 - n):
            yield seq[i:i+n]
    for slyce in get_length_n_slices(len(possible_subseq)):
        if slyce == possible_subseq:
            return True
    return False

print get_longest_common_subseq([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]])

print get_longest_common_subseq(['Oh, hello, my friend.',
                                     'I prefer Jelly Belly beans.',
                                     'When hell freezes over!'])

答案 7 :(得分:0)

我的回答很慢,但很容易理解。处理一个包含 100 个 1 kb 字符串的文件大约需要两秒钟,如果有多个,则返回任何一个最长的子字符串

ls = list()
ls.sort(key=len)
s1 = ls.pop(0)
maxl = len(s1)

#1 创建一个按长度向后排序的所有子字符串的列表。因此我们不必检查整个列表。

subs = [s1[i:j] for i in range(maxl) for j in range(maxl,i,-1)]
subs.sort(key=len, reverse=True)
    

#2 检查下一个最短的子字符串,然后是下一个等等。如果不在任何下一个最短的字符串中,则打破循环,这并不常见。如果通过所有检查,默认是最长的一次,打破循环。

def isasub(subs, ls):
    for sub in subs:
        for st in ls:
            if sub not in st:
                break 
        else:
            return sub
            break
print('the longest common substring is: ',isasub(subs,ls))

答案 8 :(得分:0)

Caveman 解决方案将根据您作为列表传递的子字符串长度为您提供一个数据框,其中包含字符串中最频繁的子字符串:

@Produces(MediaType.APPLICATION_JSON)

public Response uploadFile(@QueryParam("fotografia") String fotografia) {

    BD bd = new BD();
    int id = 54;

    try {

        String path = "/fotografias/" + id + ".jpg";

          //guardarFicheiro(fotografia, path);
          
        ConnectionBD connection = bd.abrirLigacao();
        PreparedStatement ps = connection.getConn()
                .prepareStatement("UPDATE utilizador SET fotografia=? WHERE id=?");

        ps.setString(1, fotografia);
        ps.setInt(2, id);

        int x = ps.executeUpdate();

        if (x > 0) {
            bd.fecharConexao(connection);
            return Response.ok().build();
        }

        bd.fecharConexao(connection);

    } catch (SQLException ex) {
        System.out.println(ex);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}

结果是:

import pandas as pd

lista = ['How much wood would a woodchuck',' chuck if a woodchuck could chuck wood?']

string = ''
for i in lista:
    string = string + ' ' + str(i)

string = string.lower()

characters_you_would_like_to_remove_from_string = [' ','-','_']

for i in charecters_you_would_like_to_remove_from_string:
    string = string.replace(i,'')

substring_length_you_want_to_check = [3,4,5,6,7,8]

results_list = []

for string_length in substring_length_you_want_to_check:
    for i in range(len(string)):
        checking_str = string[i:i+string_length]
        if len(checking_str) == string_length:
            number_of_times_appears = (len(string) - len(string.replace(checking_str,'')))/string_length
            results_list = results_list+[[checking_str,number_of_times_appears]]


df = pd.DataFrame(data=results_list,columns=['string','freq'])

df['freq'] = df['freq'].astype('int64')

df = df.drop_duplicates()


df = df.sort_values(by='freq',ascending=False)

display(df[:10])