Jun garlic secret documents (edit distance)

 

Everyone is a little secret, garlic king is no exception, he secretly recorded in a small book, and leave the backup, but this content is first destroyed the people, not necessarily the same as the original, and now he wants to present the shining of a second restore the contents of the present, a time to do each operation, one operation may be increased at a position of a character, a character delete, or change the position of a character other character, he wondered if he requires a minimum number of operations in order to carry out the first of the content of this reduction.

Input Format

The first line of a character string A, after the first character string represents a present is destroyed.

A second line character string B, present above represents the second string.

Strings are only lowercase letters and a length not more than 1,000.

Output Format

Output An integer operands garlic Jun minimum to do.

Sample input

aa
from

Sample Output

1 

 

This question is changed to find the B string A string edit distance, the edit distance according to the state transition equation transferred on it.

Reference another blog https://www.cnblogs.com/jiamian/p/12203580.html

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int maxn=1e5+10;
16 using namespace std;
17 
18 char a[1005];
19 char b[1005];
20 int dp[1005][1005];
21 
22 int main()
23 {
24     scanf("%s %s",a,b);
25     for(int i=1;i<=strlen(a);i++)
26         dp[i][0]=i;
27     for(int i=1;i<=strlen(b);i++)
28         dp[0][i]=i;
29     for(int i=1;i<=strlen(a);i++)
30     {
31         for(int j=1;j<=strlen(b);j++)
32         {
33             if(a[i-1]==b[j-1]) dp[i][j]=dp[i-1][j-1];
34             else dp[i][j]=min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))+1;
35         }
36     }
37     printf("%d\n",dp[strlen(a)][strlen(b)]);
38     return 0;
39 }

 

 

 

-

Guess you like

Origin www.cnblogs.com/jiamian/p/12208007.html