String editing distance:
Given two strings S and T, the minimum operand required to add, delete, and modify from S to T is called the string editing distance.
The idea of dynamic programming (DP) can be used to determine the editing distance between two strings. The basic idea of dynamic programming is to break down a complex optimal problem into a series of simple optimal problems, the simple optimal solution problem is further resolved until the optimal solution can be seen at a glance.
Make D (S, T) the distance from string s to string T. Then, the problem of D (S, T) can be converted to the length of the substring.
D (s, t) = min {d (s1... n, t1 .... m) + f (S1, T1), D (s0 ..... n, t1 ..... m) + 1, D (s1 ..... n, t0 .... m) + 1}
It can be converted to the minimum value of the solution to the three seed problem. The three sub-problems are:
1. The editing distance calculated from the second character. If S1 = T1, F (S1, T1) = 0 indicates no editing is required. If S1 is not equal to T1, F (S1, T1) = 1, indicating that one editing distance is required.
2. t starts from the second character, and s starts from the first character. The editing distance between the two is calculated. Adding 1 indicates that one editing distance is required for processing the first character of T.
3. t starts from the first character, and s starts from the second character. The editing distance between the two is calculated. Adding 1 indicates that one editing distance is required for processing the first character of S.
The Code is as follows:
# Include <iostream>
# Include <cstring>
Using namespace STD;
Int CAL (char * s, char * t)
{
If (* s = 0x0)
{
If (* t! = 0x0)
Return strlen (t );
Else
Return 0;
}
If (* t = 0x0)
{
If (* s! = 0x0)
Return strlen (s );
Else
Return 0;
}
Int s1t1 = CAL (S + 1, t + 1) + (s [0] = T [0]? 0: 1 );
Int s0t1 = CAL (s, t + 1) + 1;
Int s1t0 = CAL (S + 1, t) + 1;
Return min (s1t1, s0t1), s1t1 );
}
Int main ()
{
Cout <CAL ("youare", "Areyou ");
}