Common subsequence--poj1458 (longest common sub-sequence)

Source: Internet
Author: User

Common subsequence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 43211 Accepted: 17526

Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < X1, x2, ..., xm > another sequence Z = < Z1, Z2, ..., ZK > is a subsequence of X if the Re exists a strictly increasing sequence < I1, I2, ..., ik > of indices of X such so for all J =,..., K, Xij = Zj. For example, Z = < A, b, F, C > are a subsequence of X = < A, b, C, F, B, C > with index sequence < 1, 2, 4, 6;. Given sequences X and y the problem is to find the length of the Maximum-length common subsequence of x and Y.

Input

The program input was from the STD input. Each data set in the input contains the strings representing the given sequences. The sequences is separated by any number of white spaces. The input data is correct.

Output

For each set of data the program prints on the standard output the length of the maximum-length common subsequence from th e beginning of a separate line.

Sample Input

ABCFBC         abfcabprogramming    contest ABCD           MNP

Sample Output

420

Source

Southeastern Europe 2003 The problem is to find the length of the largest common subsequence, not the common substring, the difference is: the longest common substring must be contiguous, and the longest common subsequence does not need to be contiguous! Note the difference between the longest common substring (longest commonsubstring) and the longest common subsequence (Longestcommon subsequence, LCS): a substring (Substring) is a contiguous part of a string, A subsequence (subsequence) is a new sequence obtained by removing any element from a sequence without altering the order of the sequence, or, more simply, the position of the character of the former (substring) must be continuous, and the latter (the subsequence LCS).   For example, the longest common substring of the string ACDFG with AKDFC is DF, and their longest common subsequence is ADF. This is a bare cls. I'll just say it. The principle of CLS is to make any one string increment one character at a time to compare with another, and find out the largest public part!

LCS (s1,s2) equals the largest of the following 3 items:

(1) LCS (S1,S2 ')--if C1 is not equal to C2;

(2) LCS (S1 ', S2) --if C1 is not equal to C2;

(3) LCS (S1 ', S2 ') LCS (S1 ', S2 ') +1--if C1 equals C2;

Boundary termination Condition: If both S1 and S2 are empty strings, the result is also an empty string.

Look at a picture good understanding first let a oneself is a string, and string Bdcaba a A and b than the first comparison they are a string, at the end A and B are not equal, the longest public part is a front string (empty) and B or B front string (empty) and a the largest one, obviously they have no front, is 0 second comparison A and BD, at the end A and d are not equal, the longest public part is a front string (empty) and BD or D front string (B) and the largest one of a, obviously their public part is 0 the third time to compare a and BDC, the end A and C are unequal, The longest public part is a front string (empty) and BDC or C front string (BD) and the largest one of a, obviously their public part is 0 for the fourth time compared to a and BDCA, the end A and a are equal,   The longest public part is a front string (empty) and a front string (BDC) public length plus 1 (because the last equality is a) obviously their public part is 1 .... Writing to the end is the longest common part of the maximum length of the boilerplate template:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Chars1[ +],s2[ +];6 intdp[ +][ +];7 intMain ()8 {9     inti,j;Ten      while(SCANF ("%s%s", s1,s2)! =EOF) One     { AMemset (DP,0,sizeof(DP)); -         intlen1=strlen (S1); -         intLen2=strlen (S2); the          for(i=1; i<=len1;i++//i and J start from 1 to move the whole right down one bit, avoiding the case that the string money is empty -         { -              for(j=1; j<=len2;j++) -             { +                 if(s1[i-1]==s2[j-1]) -dp[i][j]=dp[i-1][j-1]+1; +                 Else ADp[i][j]=max (dp[i][j-1],dp[i-1][j]); at             } -         } -printf"%d\n", Dp[len1][len2]); -     } -     return 0; -}

This is a template to remember on the line, welcome to leave a message to inquire!

Common subsequence--poj1458 (longest common sub-sequence)

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.