1808: Common sub-sequence (common subsequence)

Source: Internet
Author: User

1808: Common sub-sequence
    • View
    • Submit
    • Statistics
    • Questions
Total time limit:
Memory Limit:
We call sequence Z = < Z1, Z2, ..., ZK > is sequence x = < X1, x2, ..., XM > Subsequence when and only if there is a strictly ascending sequence < I1, I2, ..., ik > , making it to J = 1, 2, ..., k, with Xij = ZJ. For example z = < A, b, F, C > is the subsequence of x = < A, b, C, F, B, C >.

Now given two sequences x and Y, your task is to find the largest common subsequence of x and Y, that is to find the longest sequence z, so that Z is both a subsequence of X and a sub-sequence of Y.
The input includes multiple sets of test data. Each group of data consists of one row, giving two strings with a length of not more than 200, representing two sequences. Two strings are separated by a number of spaces.
For each set of input data, output one row, giving the length of the maximum common subsequence of two series.
Sample input
ABCFBC         abfcabprogramming    contest ABCD           MNP
Sample output
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Charx[1001],y[1001];intc[1001][1001];intL1,l2;intMain () { while(SCANF ("%s%s", x+1, y+1)==2) {memset (c,0,sizeof(c)); L1=strlen (x+1); L2=strlen (y+1);  for(intI=1; i<=l1;i++)        for(intj=1; j<=l2;j++)         if(x[i]==Y[j]) c[i][j]=c[i-1][j-1]+1; ElseC[i][j]=max (c[i-1][j],c[i][j-1]); printf ("%d\n", C[l1][l2]);} return 0;}

1808: Common sub-sequence (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.