檔案比較演算法(1)

來源:互聯網
上載者:User
# -*- coding: cp936 -*-
'''
檔案比較演算法:
演算法模型參見:
# 參考文章:http://blog.csdn.net/clariones/archive/2006/11/19/1396880.aspx
#           http://blog.csdn.net/clariones/archive/2006/11/24/1412394.aspx
1.確定最大匹配率
2.確定最優匹配路徑
'''
left = 'ABCACADF'
right = 'BCXCADFESBABCACA'
all = []
# 建立矩陣,行數與列數均為left,right的長度+1,並將所有元素置0
for i in range(len(left) + 1):
    all.append([])
    for j in range(len(right) + 1):
        all[i].append(0)
##for i in all:
##    print i
# 比較left與right的值,相同的將矩陣中對應元素置1    
for l in range(len(left)):
    for r in range(len(right)):
        if left[l] == right[r]:
            all[l][r] = 1
##print '*'* 10
##for i in all:
##    print i
# 計算最大匹配數
for i in range(len(left) - 1,-1,-1):
    for j in range(len(right) - 1,-1,-1):
        all[i][j] = max(all[i][j+1],all[i+1][j+1]+ all[i][j],all[i+1][j])
print '*' * 20
for i in all:
    print i
# 計算最短路徑        
for j in range(len(right) - 1,-1,-1):
    for i in range(len(left) - 1,-1,-1):
        if left[i] == right[j]:
            all[i][j] = all[i + 1][j + 1] + 1
        else:
            if all[i + 1][j] >= all[i][j + 1]:
                all[i][j] = all[i + 1][j]
            else:
                all[i][j] = all[i][j+1] + 1
print '*' * 20
for i in all:
    print i

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.