Motion gauges, simulations, recursion, longest common subsequence

Source: Internet
Author: User

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

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.