Title Description Description
Set A and B are 2 strings. To convert string A to string B with a minimum of character manipulation. The character operations described here include:
(1) Delete a character;
(2) inserting a character;
(3) Change one character to another character.
The minimum character operand used to transform string A into string B is called the editing distance of the string A through B, which is recorded as D (A, A, a). Try to write the program, on the given 2 strings A and B, calculate their editing distance d (A, a, a, a,).
Enter a description input Description
The input file edit.in has two lines, the first line is string A, and the second line is string B.
outputs description output Description
The output file edit.out only one line, which is the editing distance d (A, b).
sample input to sample
Fxpimu
Xwrs
sample output Sample outputs
5
Data Size & Hint
The length of the 40% data string A and B is not more than 100;
The length of the 100% data string A, B is no more than 4000.
The other is a DP, we define DP[I][J] as the first string of the previous I character, the second string of the first J characters of the editing distance. The state transition equation is as follows:
The first two parts we can do when initializing the DP array. When S1[i]!=s2[j]: dp[i-1][j]+1 means delete s1[i],dp[i][j-1]+1 means add s2[j],dp[i-1][j-1]+1 means modify S1[i] to s2[j].
The code is as follows :
#include <cstdio> #include <cstring> #include <iostream> using namespace
Std
int dp[4010][4010];
int main () {string A, B;
cin>>a>>b;
int Lena=a.length ();
int Lenb=b.length ();
printf ("%d%d\n", LENA,LENB);
dp[0][0]=0;//Initialize for (int i=0;i<lena;i++) dp[i+1][0]=i+1;//initialize for (int i=0;i<lenb;i++) dp[0][i+1]=i+1;//initialization for (int i=0;i<lena;i++) for (int j=0;j<lenb;j++) if (A[i]==b[j]) dp[i+1][j+1]=dp[i][j];//need to be from
DP[1][1] Starts the transfer, so the subscript is all plus one else dp[i+1][j+1]=min (Dp[i][j]+1,min (dp[i][j+1]+1,dp[i+1][j]+1));
printf ("%d", Dp[lena][lenb]);
return 0; }/* RCPW WBL */