亚洲精品不卡AV在线播放|国产性爱无码高清视频|国产成人 ,97人人色,国产免费一区二区三区,日本一区二区在线

內(nèi)容中心

返回列表
2026年口碑好的寧波噴霧注塑機(jī)/臥式注塑機(jī)哪家專(zhuān)業(yè)制造廠家實(shí)力參考
2026-02-08 07:06:47

To solve the problem of finding the minimum number of operations (insertion, deletion, substitution) to convert string A to string B, we use the Levenshtein Distance algorithm, a classic dynamic programming approach.

Key Insight: Dynamic Programming (DP)

The problem can be broken down into smaller subproblems where we compute the minimum operations for substrings of A and B. We define dp[i][j] as the minimum operations to convert the first i characters of A to the first j characters of B.

Approach

  1. Base Cases:

    • If A is empty (i=0), we need j insertions to get B: dp[0][j] = j.
    • If B is empty (j=0), we need i deletions to get B: dp[i][0] = i.
  2. Recursive Case:

    • If the i-th character of A equals the j-th character of B (A[i-1] == B[j-1]), no operation is needed: dp[i][j] = dp[i-1][j-1].
    • If not, take the minimum of three options:
      • Replace: dp[i-1][j-1] +1 (replace A[i-1] with B[j-1]).
      • Delete: dp[i-1][j] +1 (delete A[i-1]).
      • Insert: dp[i][j-1] +1 (insert B[j-1] into A).

Solution Code

Standard DP (O(mn) Time, O(mn) Space)

def min_edit_distance(A: str, B: str) -> int:
    m, n = len(A), len(B)
    dp = [[0]*(n+1) for _ in range(m+1)]

    # Base cases
    for i in range(m+1):
        dp[i][0] = i
    for j in range(n+1):
        dp[0][j] = j

    # Fill DP table
    for i in range(1, m+1):
        for j in range(1, n+1):
            if A[i-1] == B[j-1]:
                dp[i][j] = dp[i-1][j-1]
            else:
                dp[i][j] = min(
                    dp[i-1][j-1] +1,  # Replace
                    dp[i-1][j] +1,    # Delete
                    dp[i][j-1] +1     # Insert
                )
    return dp[m][n]

Optimized DP (O(mn) Time, O(min(m,n)) Space)

We use a 1D array since each row only depends on the previous row:

def min_edit_distance_optimized(A: str, B: str) -> int:
    if len(A) < len(B):
        return min_edit_distance_optimized(B, A)  # Ensure B is shorter

    m, n = len(A), len(B)
    dp = list(range(n+1))  # Base case: i=0

    for i in range(1, m+1):
        prev = dp[0]  # dp[i-1][0]
        dp[0] = i     # Base case: j=0 for current i

        for j in range(1, n+1):
            temp = dp[j]  # dp[i-1][j]
            if A[i-1] == B[j-1]:
                dp[j] = prev
            else:
                dp[j] = min(prev+1, dp[j]+1, dp[j-1]+1)
            prev = temp  # Update prev to dp[i-1][j] for next j

    return dp[n]

Explanation

  • Standard DP: The 2D table stores all subproblem solutions, making it easy to understand and implement.
  • Optimized DP: Uses a 1D array to reduce space complexity. It tracks the previous row's values and updates the array in place.

Both approaches efficiently compute the minimum edit distance, with the optimized version being better for longer strings. For example:

  • min_edit_distance("kitten", "sitting") returns 3 (replace 'k'→'s', 'e'→'i', insert 'g').

This solution is widely used in applications like spell checkers, DNA sequence alignment, and natural language processing.

Answer: The minimum number of operations is calculated using the Levenshtein Distance algorithm as described. For example, if the input strings are "kitten" and "sitting", the answer is 3. The general solution is implemented via dynamic programming as shown above.



(免責(zé)聲明:本文為本網(wǎng)站出于傳播商業(yè)信息之目的進(jìn)行轉(zhuǎn)載發(fā)布,不代表本網(wǎng)站的觀點(diǎn)及立場(chǎng)。本文所涉文、圖、音視頻等資料的一切權(quán)利和法律責(zé)任歸材料提供方所有和承擔(dān)。本網(wǎng)站對(duì)此資訊文字、圖片等所有信息的真實(shí)性不作任何保證或承諾,亦不構(gòu)成任何購(gòu)買(mǎi)、投資等建議,據(jù)此操作者風(fēng)險(xiǎn)自擔(dān)。) 本文為轉(zhuǎn)載內(nèi)容,授權(quán)事宜請(qǐng)聯(lián)系原著作權(quán)人,如有侵權(quán),請(qǐng)聯(lián)系本網(wǎng)進(jìn)行刪除。

點(diǎn)擊呼叫(詳情介紹)
在線客服

在線留言
您好,很高興為您服務(wù),可以留下您的電話或微信嗎?