如何删除两个字符串之间的冗余匹配?

时间:2012-09-14 07:56:21

标签: string algorithm match

给定两个字符串str1和str2我有一个匹配列表,描述共享子串作为[str1_beg,str1_end,str2_beg,str2_end]形式的间隔。我想删除冗余匹配,其中来自匹配的str1_beg,str1_end和str2_beg,str2_end嵌入在其他匹配中。

2 个答案:

答案 0 :(得分:0)

对于每个[beg_index,end_index]找到[beg_index_new,end_index_new]并删除满足end_index< end_index_new和beg_index> = beg_index_new。

那是O(n ^ 2)

答案 1 :(得分:0)

首先,您可以更有效地存储比赛。

[str_beg,str2_beg,match_len]

这也可以很容易地检查冗余,例如

for match in matches:
  for i in xrange(len(matches)):
    if matches[i][:2] == match[:2] and mathches[i][2] < match[2]:
      del matches[i]

我假设你的匹配列表被分配给一个名为matches的变量,并且具有我上面提出的结构,所以ma。我正在使用&lt;运算符而不是&lt; =运算符,因为在它们相等的情况下,它们是完全相同的匹配,并且我假设你不会有两次相同的匹配。 我正在检查两个matche的[:2]切片,我是他们列表的前两个元素,这是起始位置。