Dynamic Programming to solve the problem of distance string

Source: Internet
Author: User

/*

Question:
There is a string X, which is an extension string of X after inserting any number of spaces in the header and tail of X,
If the string X is "abcbcd", the strings "abcb □cd", "□a □bcbcd □" and" abcb □cd □" are all extension strings of X,
Here "□" represents space characters.
If A1 is an extension string of string A, B1 is an extension string of string B, and A1 and B1 have the same length,
Then I will define the distance between the string A1 and B1 as the sum of the distance between the characters in the corresponding position, rather than two spaces.
The distance between characters is defined as the absolute value of their ASCII code difference, while
The distance is a known value K, and the distance between the space character and the space character is 0. In all the extended strings of string a and string B,
There must be two extended extension strings A1 and B1, so that the distance between A1 and B1 reaches the minimum. We define this distance as the distance between string a and string B.
*/

Optimal sub-structure:

Function D () is the distance between two characters or strings.
The function L () is the length of the string.
K is the length of space characters and non-space characters

D (str1, str2) =
L (str1) * k When str1 is not an empty string, str2 is an empty string
L (str2) * k When str2 is not an empty string, str1 is an empty string
Min (D (str1 [1], str2 [1]) + d (str1 [2... n], str2 [2... n]), K + d (str1, str2 [2... n]), K + d (str1 [2... n], str2 ))

# Include "stdlib. H"
# Include "stdio. H"
# Include "string. H"
# Include "math. H"

# Define K 2/* distance between spaces and other characters */

Char A [101];/* string */
Char B [101];/* string B */
Char extenda [201];/* extension string of string */
Char extendb [201];/* extension string of string B */
Int distance [101] [101];/* distance from the recorded string */
Int construct [101] [101];/* extended string used to construct a and B */

/* Compare the size of three numbers and return the subscript of the minimum number (starting from 0 )*/
Int getminindex (int A, int B, int C)
{
If (A <B)
{
If (C <)
Return 2;
Else
Return 0;
}
Else/* B <= */
{
If (C <B)
Return 2;
Else
Return 1;
}
}

/* Return the distance between two characters according to the rule */
Int getchardistance (char a, char B)
{
If (A = 32 & B! = 32) | (! = 32 & B = 32 ))
Return K;
Else
Return ABS (a-B );
}

/* Calculate and print the distance between two strings */
Void getstrdistance (char * a, char * B)
{
Int I, j, P, temp [3], minindex, Dir;
Int M = strlen ();
Int n = strlen (B );

/* Initialize */
For (I = 0; I <= m; I ++)
Distance [I] [0] = I * K;
For (j = 0; j <= N; j ++)
Distance [0] [J] = J * K;
Extenda [200] = '/0 ';
Extendb [200] = '/0 ';

/* Bottom-up solution */
For (I = 1; I <= m; I ++)
For (j = 1; j <= N; j ++)
{
/* Note the subscript of the string and the subscript of the table */
Temp [0] = getchardistance (A [I-1], B [J-1]) + distance [I-1] [J-1];
Temp [1] = getchardistance (A [I-1], '') + distance [I-1] [J];
Temp [2] = getchardistance ('', B [J-1]) + distance [I] [J-1];
Construct [I] [J] = minindex = getminindex (temp [0], temp [1], temp [2]);
Distance [I] [J] = temp [minindex];
}

/* Construct an extension string from the back to the front */
I = m ;;
J = N;
P = 199;
While (I> 0 | j> 0)
{
Dir = construct [I] [J];
If (DIR = 0)
{
Extenda [p] = A [-- I];
Extendb [p] = B [-- J];
}
Else if (DIR = 1)
{
Extenda [p] = A [-- I];
Extendb [p] = '';
}
Else
{
Extenda [p] = '';
Extendb [p] = B [-- J];
}
P --;
}

/* Move the string */
For (I = 0; I <200-p; I ++)
{
Extenda [I] = extenda [I + p + 1];
Extendb [I] = extendb [I + p + 1];
}

/* Print the extension string between the distance and A and B */
Printf ("the distance of % s and % s is % d/N", a, B, distance [m] [N]);
Printf ("the extend-string of A is :");
For (I = 0; I <200-p-1; I ++)
Printf ("% C", extenda [I]);
Printf ("/N ");
Printf ("the extend-string of B is :");
For (I = 0; I <200-p-1; I ++)
Printf ("% C", extendb [I]);
}
 
Int main ()
{
Printf ("Please input the two strings the length of which are shorter than 101:/N ");
Printf ("string a is :");
Scanf ("% s", );
Printf ("string B is :");
Scanf ("% s", B );
Getstrdistance (A, B );
 
System ("pause ");
Return 0;
}
 

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.