[Programming question] The longest Public String

Source: Internet
Author: User

56. The longest Public String (algorithm, string ).
Question: If all the characters in string 1 appear in the order of the strings in another string,
Then, string 1 is called a substring of string 2.
Note that the character of a substring (string 1) must appear in string 2 consecutively.
Compile a function, enter two strings, calculate their longest public substrings, and print the longest public substrings.
For example, input two strings: bdcaba and abcbdab. The strings bcba and bdab are both the most
Long Public substrings are output with a length of 4 and any substring is printed.

 

Typical dynamic planning questions.

# Include <stdio. h> # include <stdlib. h> # include <string. h> void printlcs (char * X, int M, int N, int I, Int J, char * B) {if (I <0 | j <0) {return;} Char B = * (B + I * n + J); If (B = 'y') {printlcs (x, M, N, I-1, j-1, B); printf ("% C", X [I]); // obtain the following common letters first, it should be printed later} else if (B = 'U') {printlcs (x, M, N, I-1, J, B );} else {printlcs (x, M, N, I, j-1, B) ;}} int LCS (char * X, char * Y, int M, int N, char * & B) // returns the maximum length of Public substrings {int * c = (int *) malloc (m + 1) * (n + 1) * sizeof (INT); // records the longest common subsequence of each subproblem 0-m 0-n B = (char *) malloc (M * n * sizeof (char); // mark 1-m 1-N memset (C, 0, (m + 1) * (n + 1) * sizeof (INT); // Initialization is all 0 for (INT I = 0; I <m; I ++) {int * crow_1 = C + I * (n + 1 ); // note that in C, I = 1 j = 1 corresponds to X [0] of X and Y [0] int * crow = C + (I + 1) of Y) * (n + 1); char * brow = B + I * N; // note that in B, I = 0 J = 0 corresponds to X [0] of X and Y [0] For (Int J = 0; j <N; j ++) {If (X [I] = Y [J]) {crow [J + 1] = crow_1 [J] + 1; brow [J] = 'y';} else {crow [J + 1] = (crow_1 [J + 1]> crow [J])? Crow_1 [J + 1]: crow [J]; brow [J] = (crow_1 [J + 1]> crow [J])? 'U': 'l' ;}} printlcs (x, m, n, m-1, n-1, B ); int maxlen = * (C + M * (n + 1) + n); free (c); free (B); Return maxlen;} int main () {char X [7] = {'A', 'B', 'C', 'B', 'D', 'A', 'B '}; char y [6] = {'B', 'D', 'C', 'A', 'B', 'A'}; char * B = NULL; int L = LCS (X, Y, 7, 6, B); 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.