Longest common sub-sequence

Source: Internet
Author: User

Problem description
We call the sequence Z = < Z1, Z2, ..., ZK > is sequence x = < X1, x2, ..., the subsequence of XM > When and only if there is a strictly ascending sequence < I1, I2, ..., IK, make to J = 1, 2, ..., K, There is 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.
Input data
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.
Output requirements
For each set of input data, output one row, giving the length of the maximum common subsequence of two series.
Input sample
ABCFBC Abfcab
Programming Contest
ABCD MNP
Output sample
4
2
0

Import Java.util.Scanner; Public classcommonsubsequence { Public Static void Main(string[] args) {intDp[][] =New int[ -][ -]; Scannerinch=NewScanner (System.inch); String A =inch. Next (); String B =inch. Next ();intA = A.length ();intb = B.length (); for(inti =1; I <= A; i++) { for(intj =1; J <= B; J + +) {if((A.charat (i1) -' 0 ') = = (B.charat (J-1) -' 0 ') {Dp[i][j] = dp[i-1][j-1] +1; }Else{Dp[i][j] = Math.max (Dp[i-1][J], Dp[i][j-1]); }}} System. out. println (Dp[a][b]); }}

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.