Skip to content

Instantly share code, notes, and snippets.

@nlpjoe
Last active March 18, 2018 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlpjoe/2ccaed18a6dcb0db3ca0173e3be3bd21 to your computer and use it in GitHub Desktop.
Save nlpjoe/2ccaed18a6dcb0db3ca0173e3be3bd21 to your computer and use it in GitHub Desktop.
[编辑距离Damerau–Levenshteindistance扩展]#python
class EditDistance(object):
    def __init__(self, allow_transpose=True):
        self.allow_transpose = allow_transpose
        self.score = None

    def score_edit_distance(self, source, target):
        if source == target:
            return 0
        s_pos = len(source)
        t_pos = len(target)
        self.clear(s_pos, t_pos)
        for i in range(s_pos + 1):
            for j in range(t_pos + 1):
                b_score = self.score[i][j]
                if b_score != self.worse():
                    continue
                if i == 0 and j == 0:  # 0,0位置为空,默认为正确
                    b_score = self.best()
                else:
                    if i > 0:  # 删除权重
                        b_score = min(b_score, self.score[i-1][j] + self.delete_cost(source[i-1]))
                    if j > 0:  # 插入权重
                        b_score = min(b_score, self.score[i][j-1] + self.insert_cost(target[j-1]))
                    if i > 0 and j > 0:  # 替换权重
                        b_score = min(b_score, self.score[i-1][j-1] + self.substitute_cost(source[i-1], target[j-1]))
                    if i > 1 and j > 1:  # 置换权重
                        b_score = min(b_score, self.score[i-2][j-2] + self.transpose_cost(source[i-2], source[i-1], target[j-2], target[j-1]))
                self.score[i][j] = b_score
        return self.score[s_pos][t_pos]

    def clear(self, source_length, target_length):
        if self.score is None or len(self.score) <= source_length or len(self.score[0]) <= target_length:
            self.score = [[self.worse() for row in range(target_length+1)] for col in range(source_length+1)]

    def worse(self):
        return float('inf')  # 正无穷

    def best(self):
        return 0.0

    def unit(self):
        return 1.0

    def insert_cost(self, o):
        return self.unit()

    def delete_cost(self, o):
        return self.unit()

    def substitute_cost(self, source, target):
        return self.best() if source == target else self.unit()

    def transpose_cost(self, s1, s2, t1, t2): # 交换相邻的两个元素
        if s1 == t2 and s2 == t1:
            return self.unit() if self.allow_transpose else 2 * self.unit()
        return self.worse()


if __name__ == '__main__':
    ed = EditDistance()
    print(ed.score_edit_distance('', ''))
    print(ed.score_edit_distance('happy', ''))
    print(ed.score_edit_distance('happy', 'hpapy'))
def solution(a, b):
    num_b = [int(x) for x in str(b)]
    num_a = [int(x) for x in str(a)]
    map_1 = [[a, b] for a, b in zip(num_a, num_b)]
    cnt = 0
    for i in range(len(map_1)):
        for j in range(i+1, len(map_1)):
            if map_1[i][0] == map_1[j][1] and map_1[i][1] == map_1[j][0]:
                cnt += 1
                num_a[i] = 0
                num_a[j] = 0
                num_b[i] = 0
                num_b[j] = 0
    for i in range(len(num_a)):
        a_b = int(num_a[i]) - int(num_b[i])
        b_a = int(num_b[i]) - int(num_a[i])
        cnt += max(a_b, b_a)
    return cnt



while True:
    num1 = input()
    num2 = input()
    print(solution(num1, num2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment