Pku1458 longest Public String DP

Source: Internet
Author: User

Common subsequence

Time limit:

1000 ms

 

Memory limit:

10000 K

Total submissions:

20435

 

Accepted:

7752

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 there exists a strictly increasing sequence <I1, I2 ,..., ik> of indices of X such that for all j = 1, 2 ,..., k, Xij
= ZJ. for example, Z = <A, B, F, C> is a subsequence of X = <A, B, C, F, B, c> with index sequence <1, 2, 4, 6>. given two 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 is from the STD input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

Output

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

Sample Input

Abcfbc
Abfcab

Programming
Contest

ABCD
MNP

Sample output

4

2

0

 

 

 

DP

Classic applications
:

Longest Public String
.

Transfer Equation
:
(RES [I] [J]:

Indicates
Stringa

Before
I

Characters and
Stringb

Before
J

Maximum length of Public strings
)

Res [I] [J] = 0


(I = 0 | j = 0)

Res [I] [J] = res [I-1] [J-1] + 1


(Stringa [I] = stringb [J])

Res [I] [J] = max (RES [I] [J-1], Res [I-1] [J])
(Stringa [I]! = Stringb [J])

 

The Code is as follows:
:

 

# Include <stdio. h> <br/> # include <string. h> <br/> # define maxn 1005 <br/> int main () <br/> {<br/> int I, j, La, LB, res [maxn] [maxn]; char a [maxn], B [maxn]; <br/> while (scanf ("% S % s", a, B )! = EOF) <br/>{< br/> memset (Res, 0, sizeof (RES); <br/> La = strlen (); lb = strlen (B); <br/> for (I = 0; I <la; ++ I) <br/> for (j = 0; j <Lb; ++ J) <br/> if (a [I] = B [J]) res [I + 1] [J + 1] = res [I] [J] + 1; <br/> else res [I + 1] [J + 1] = res [I] [J + 1]> res [I + 1] [J]? Res [I] [J + 1]: res [I + 1] [J]; <br/> printf ("% d/N ", res [la] [LB]); <br/>}< br/> return 0; <br/>}< br/>

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.