Problem B (subsequence problem)

Source: Internet
Author: User
Problem descriptiona subsequence of a given sequence is the given sequence with some elements (possible none) left out. given a sequence X = & lt; x1, x2 ,..., XM & gt; another sequence z = & lt; Z1, Z2 ,..., ZK & gt; Is a subsequence of X if there exists a strictly increasing sequence & lt; I1, I2 ,..., ik & gt; of indices of X such that for all j = 1, 2 ,..., k, Xij = ZJ. for example, Z = & lt; A, B, F, C & gt; Is a subsequence of X = & lt; A, B, C, F, B, C & gt; with index sequence & lt; 1, 2, 4, 6 & gt ;. given two sequences x and y the problem is to find the length of the maximum-length common subsequence of X and Y. <br> the program input is from a text file. each data set in the file contains two strings representing the given sequences. the sequences are separated by any number of white spaces. the input data are correct. for each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line. <br>

 

Sample inputabcfbc abfcabprogramming contest abcd mnp sample output420 question: two strings are given to identify the maximum number of identical characters in the two strings. Idea: Set the string a [0... n], B [0... m], string a corresponds to the row of the Two-dimensional array num, string B Corresponds to the column of the Two-dimensional array num. Then the status code after each record comparison:

# Include <iostream>
# Include <cstring>
Using namespace STD;

Char str1 [1005];
Char str2 [1005];
Int DP [1005] [1005];

Int main ()
{
Int I, J;
Int len1, len2;
While (CIN> str1> str2)
{
Len1 = strlen (str1 );
Len2 = strlen (str2 );
For (I = 0; I <len1; I ++)
{
DP [I] [0] = 0;
}
For (I = 0; I <len2; I ++)
{
DP [0] [I] = 0;
}
For (I = 1; I <= len1; I ++)
{
For (j = 1; j <= len2; j ++)
{
If (str1 [I-1] = str2 [J-1])
{
DP [I] [J] = DP [I-1] [J-1] + 1;
}
Else
{
DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1]);
}
}
}
Cout <DP [len1] [len2] <Endl;
}
Return 0;
}

There are multiple ways to do this.

 

Problem B (subsequence problem)

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.