Topic three question 12th

Source: Internet
Author: User

1. Title Number: 1002

2. Simple test instructions: give two substrings, then compare substrings to get the maximum length of the characters equal to two substrings, that is, the longest common subsequence.

3. The process of thinking: in the dynamic planning of the topic, think of the dynamic planning method to do, first of all to find sub-problem, assuming that there are two strings a=a0,a1,a2,... am-1,b=b0,b1,b2...bn-1. If am-1==bn-1, the current longest common subsequence is a0,a1,... the length of the longest common subsequence of am-2 and B0,b1,b2...bn-2 plus 1, and if am-1!=bn-1, the longest common subsequence is max (a0,a1,... am-2 and B0, B1,b2...bn-1 Common subsequence, A0,a1...am-1, and b0,b1,b2...bn-2 sub-sequences) are recursively represented: if (A[i-1]==b[j-1]) {dp[i][j]=dp[i-1][j-1]+1;} Else{dp[i][j]=max (Dp[i-1][j],dp[i][j-1]);}

4. Sentiment: This problem is like a template, a meeting, as long as the problem is to find the state transfer equation set =.=

5.AC of code:

#include <iostream>
#include <cstring>
using namespace Std;
Char a[1000];
Char b[1000];
int dp[1000][1000];
int max (int l,int s) {
if (l>=s)
return l;
Else
return s;
}
int main () {
while (cin>>a>>b) {
int A1=strlen (a);
int B1=strlen (b);
Memset (Dp,0,sizeof (DP));
for (int i=1;i<a1+1;i++) {
for (int j=1;j<b1+1;j++) {
if (A[i-1]==b[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[a1][b1]<<endl;
}
return 0;
}

Original question:

Problem Description


A 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 =,..., 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 sequences X and y the problem is to find the length of the Maximum-length common subsequence of x and Y. <br& Gt The program input was from a text file. Each data set in the file contains the strings representing the given sequences. The sequences is separated by any number of white spaces. The input data is correct. 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. <br>



Sample Input


ABCFBC abfcabprogramming contest ABCD MNP



Sample Output


420




Topic three question 12th

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.