English 中文(简体)
将两条插图变成一条
原标题:making two strings into one

我要说的是,我有两条str。

AAABBBCCCCC

以及

AAAABBBBCCCC

鉴于我只能去除我应当删除的特性,因此,尽可能使这种说法相似。

  • delete the last C from the first string
  • delete the last A 以及 the last B from the second string,

以便

AAABBBCCCC

找到从每一条指示中删除哪些特性的高效算法是什么?

目前,我大米.忙地思考一下,有人把扼子放在一边的孤独,看着他们,一把其他扼子。

最佳回答

Levenshtein gap,可计算你需要多少改动才能将一个扼杀变成另一个。 对来源的微小改动,而且你可能不仅有距离,而且需要转换。

问题回答

How about using difflib?

import difflib

s1 =  AAABBBCCCCC 
s2 =  AAAABBBBCCCC 

for difference in difflib.ndiff(s1, s2):
    print difference,
    if difference[0] ==  + :
        print  remove this char from s2 
    elif difference[0] ==  - :
        print  remove this char from s1 
    else:
        print  no change here 

这将勾销你可用来消除分歧的两条插图之间的差别。 产出如下:

  A no change here
  A no change here
  A no change here
+ A remove this char from s2
+ B remove this char from s2
  B no change here
  B no change here
  B no change here
  C no change here
  C no change here
  C no change here
  C no change here
- C remove this char from s1

Don t know if it s the fastest, but as code goes, it is at least short:

import difflib
  .join([c[-1] for c in difflib.Differ().compare( AAABBBCCCCC , AAAABBBBCCCC ) if c[0] ==    ])

I think regular expression can do this.It s a string distance problem. I mean. Let s have two string:

str1 =  abc 
str2 =  aabbcc 

首先,我选择简短,并作如下常规表述:

regex =  (w*) + (w*) .join(list(str1))+ (w*) 

Then, we can search:

matches = re.search(regex,str2)

I use round brackets to group the section I am interested. these groups of matches.group() is the distance of two strings.Next, I can figure out what characters should be removed. It s my idea, I hope it can help you.





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签