leetcode brush title 2019-5-9

Fifty-one out to play a few days, no time to brush the question, but also insisted today. Yesterday written to help a friend, able to get the minimum edit distance algorithm, which is a classic DP algorithm, online explanations are many and complex, I understand used to write about.

Problem Description :

A member of a magazine is writing the check, is responsible for correcting the article inside the wrong sentences typo, we want to implement a program to count the amount of work A day. A operation is divided into three categories: change a word, delete or add a word a word, we need to pass the article A statistical comparison of before and after check how many times the minimum required to operate. For simplicity, we assume that each row of articles contain only numbers and letters without spaces and other special characters.

输入描述:
	每一行输入为正整数N,表示文章的总行数(0<N<=10000)
	后面N行,为校验前的文章
	再后面N行,为校验后的文章

示例1:

输入
    2
	abcdef
	123456
	bcdg
	234567
输出
	5
	
说明
	删除了a,用g替换了e,删除了f,删除了1,增加了7,共操作5次

Problem-solving ideas:

  • The main topic is the minimum edit distance calculation times, if we write a function to calculate the minimum edit distance, called multiple times, each time to return the results of the shortest distance, can be summed output. The question is how to calculate the edit distance of two strings, for example, now has two strings s = a b c d e f s=abcdef and t = b c d g t=bcdg , how do we know how much they edit distance is the minimum?
  • So we must come up with a weapon of dynamic programming, and we do not care about his process is kind of how we are only concerned with the shortest distance results. Dynamic programming method is to decompose the problem forward, trying to solve a problem, you need to solve the problem of sub-problems, then the child to solve the problem, but also need to address the problem of sub-sub-problems. By first solving the smallest sub-problems, and then continue to the next level to solve the problem, then we can solve the ultimate problem.
  • Dynamic programming routine is most often defined status and turn to find the state transition equation.
First, define the problem:

dp[i[j] 代表着 s 中前 i 个字符转移到 t 中前 j 个字符的最小编辑距离

State transition formula:

Do a law look two-dimensional array, we first fill out an array of boundary conditions, when s = 0 s=0 At this time the source string is empty, to reach the target string t t minimum edit distance is the length of the string itself, the operation is carried out continually adding a character. Therefore, the minimum edit distance dp [0] [j] is J; if t = 0 t=0 At this time the target string transferred to the target string is empty, the source character string is deleted, the number of how many deleted, so dp [i] [0] is the minimum edit distance i.

b c d g
0 1 2 3 4
a 1
b 2
c 3
d 4
e 4
f 4
  • Next we try from dp [1] [1], at this time dp [1] [1] There are three sources of direction, namely dp [0 [0], dp [0] [1], dp [1 ] [0], they represent different meanings. At this time, the title is described in three operations:
    • To delete a character
      • delete a character into s t, i.e. dp [1] [1] = d [0] [1] +1
    • Add a character
      • s to add a character to become t, t means delete a character, that is, dp [1] [1] = d [1] [0] +1
    • Replace a character
      • delete a character into s t, i.e. dp [1] [1] = d [0] [0] +1
  • We should try to think about this problem, since there are three operations, then it should also be the case without action. when s [ i ] = = t [ j ] s[i]==t[j] when the mean s and t i-th character in the j-th character are equal, then dp [i] [j] =e.g. i = 2 , j = 1 i=2,j=1 Shi s = a b , t = b s=ab,t=b last time is equal to s and t, dp [2] [1]

Reference picture
At this table will become such

b c d g
0 1 2 3 4
a 1 1 \color{blue}{1} 2 3 4
b 2 1 2 3 4
c 3 2 1 2 3
d 4 3 2 1 2
e 5 4 3 2 2
f 6 5 4 3 3 \color{red}{3}

code show as below:

def mearch(s,t):
    dp = [[0]*(len(t)+1) for _ in range(len(s)+1)]
    for i in range(len(s)+1):
        dp[i][0] = i
    for i in range(len(t)+1):
        dp[0][i] = i
    for i in range(1,len(s)+1):
        for j in range(1,len(t)+1):
            if s[i-1] == t[j-1]:
                dp[i][j] = dp[i-1][j-1]
            else:
                dp[i][j] = min(dp[i-1][j-1], dp[i][j-1],dp[i-1][j])+1
    return dp[len(s)][len(t)]

n = int(input())
res = 0
origin = list()
for i in range(n):
    origin.append(input())
after = list()
for i in range(n):
    after.append(input())
for i in range(n):
    res += mearch(origin[i], after[i])
print(res)

Guess you like

Origin blog.csdn.net/weixin_43615373/article/details/90042180