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 = <x1, x2, ..., xm> Another sequence Z = <z1, Z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, I2, ..., ik> of indices of X such that for all J =,..., K, Xij = ZJ. For example, Z = <a, B, F, c> is 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. <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