Longest Common subsequence

Source: Internet
Author: User

Longest Common subsequence

 

1210: maximum public subsequence Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 10 Solved: 8
[Submit] [Status] [Web Board] Description

Give two strings and find the longest common sub-sequence length: each character in the sub-sequence can be found in the two original strings, the order of each character is the same as that of the original string.
For example:
ABCBDAB
BDCABA
Their Longest Common subsequence is BCBA with a length of 4.

 

Input

Each group contains two strings of less than 100 characters.

 

Output

The length of the output Longest Common subsequence.

 

Sample Input
ABCBDAB BDCABA
Sample Output
4
#include 
 
  #include 
  
   #include #include 
   
    using namespace std; int main(){    string str1,str2;    int dp[200][200];    while(cin>>str1>>str2)    {        memset(dp,0,sizeof(dp));         int la = str1.length();        int lb = str2.length();         for(int i = 1; i <= la; i++)            for(int j = 1; j <= lb; j++)        {            if(str1[i - 1] == str2[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<
    
     

 

 

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.