Title Link: http://poj.org/problem?id=1458
Problem Solving Report:
1. Use a two-dimensional array to simulate the longest common subsequence corresponding to each substring on two strings.
2, it is clear that the two-dimensional array is required to the right of the number
3. Recursive formula:
if (s1[i-1]==s2[j-1]) maxlen[i][j]=maxlen[i-1][j-1]+1 ; Else Maxlen[i][j]=max (maxlen[i][j-1],maxlen[i-1][j]);
Memory: 1024K time: 0MSLanguage: C + +Result: Accepted
#include <iostream>#include<string.h>#include<algorithm>#defineMAX 1000using namespacestd;CharS1[max];CharS2[max];intMaxlen[max][max];///Maxlen[i][j] Represents the S1 to the left of the first I character, the length of the longest common subsequence of a substring formed by the J character to the left of S2intMain () { while(cin>>s1>>S2) { intlen1=strlen (S1); intLen2=strlen (S2); ///The result is a request for MAXLEN[LEN1][LEN2]; Here is a recursive method of moving the rules inti,j; for(i=0; i<=len1;i++) maxlen[i][0]=0; for(j=0; j<=len2;j++) maxlen[0][j]=0; for(i=1; i<=len1;i++) { for(j=1; j<=len2;j++) { if(s1[i-1]==s2[j-1])///Here is a comparison of the first character of S1 with the S2maxlen[i][j]=maxlen[i-1][j-1]+1; ElseMaxlen[i][j]=max (maxlen[i-1][j],maxlen[i][j-1]); }} cout<<maxlen[len1][len2]<<Endl; } return 0;}
Motion gauges, simulations, recursion, longest common subsequence