Evaluate the string editing distance

Source: Internet
Author: User

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 ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.