Week F question POJ 1458 (longest common sub-sequence)

Source: Internet
Author: User

F-fTime limit:1000MS Memory Limit:10000KB 64bit IO Format:%i64d &%i64 U  

Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < X1, x2, ..., xm > another sequence Z = < Z1, Z2, ..., ZK > is a subsequence of X if the Re exists a strictly increasing sequence < I1, I2, ..., ik > of indices of X such so for all J =,..., k, X ij = Zj. For example, Z = < A, b, F, C > are a subsequence of X = < A, b, C, F, B, C > with index sequence < 1, 2, 4, 6;. Given sequences X and y the problem is to find the length of the Maximum-length common subsequence of x and Y.

Input

The program input was from the STD input. Each data set in the input contains the strings representing the given sequences. The sequences is separated by any number of white spaces. The input data is correct.

Output

For each set of data the program prints on the standard output the length of the maximum-length common subsequence from th e beginning of a separate line.

Sample Input

ABCFBC         abfcabprogramming    contest ABCD           MNP

Sample Output

420


The puzzle: give you two strings to find their longest common subsequence. This problem has been done many times, although added something, or not write right, or the LCS understand enough.
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Chara[1100],b[1100];intdp[1100][1100];intMain () { while(SCANF ("%s%s", A, b)! =EOF) {        intx=strlen (a); inty=strlen (b);  for(intI=1; i<=x;i++)        {             for(intj=1; j<=y;j++)            {                if(a[i-1]==b[j-1]) Dp[i][j]=dp[i-1][j-1]+1; ElseDp[i][j]=max (dp[i-1][j],dp[i][j-1]); }} cout<<dp[x][y]<<Endl; }    return 0;}

Week F question POJ 1458 (longest common sub-sequence)

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.