C + + Dynamic programming the longest Childe sequence instance _c language

Source: Internet
Author: User

In this paper, the longest master sequence solution for C + + dynamic programming is described. Share to everyone for your reference. The specific analysis is as follows:

Problem Description:

Find the length of the longest childe sequence in two strings.

Input:

Csblog
Belong

Output:

Max length = 4

Implementation code:

#include <stdio.h>
#include <string.h>
int arr[200][200];
/* Represents the length of the longest childe sequence of the front I and str2 of the str1 * *
int main ()
{
 char str1[100],str2[100];
 /* Input Data * *
 scanf ("%s%s", str1,str2);
 int len1 = strlen (str1);
 int len2 = strlen (str2);
 /* Initialize array *
 /int i,j;
 for (i = 0; I <= len1. ++i)
 {for
  (j = 0; J <= len2; ++j)
   arr[i][j] = 0;
 }
 /* COMPUTE * *
 for (i = 1; I <= len1 ++i)
 {
  for (j = 1; j <= len2; ++j)
  {/
   * characters are the same, then the longest childe sequence length plus 1 *
   /if (str1[i-1] = = Str2[j-1])
   {
    arr[i][j] = arr[i-1][j-1] + 1;
   }
   else///The
   current character is not the same, then take the last selected maximum as the current result/*
    Arr[i][j]=arr[i][j-1]>arr[i-1][j]?arr[i][j-1]:arr[i-1][j ];
   }
  }
 }
 /* Output result
 /printf ("max length =%d\n", Arr[len1][len2]);
 return 0;
}

I hope this article will help you with the C + + program design.

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.