Poj 1458 common subsequence

Source: Internet
Author: User

Common subsequence
Time limit:1000 ms   Memory limit:10000 K
Total submissions:39058   Accepted:15739

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         abfcabprogramming    contest abcd           mnp

Sample output

420

Question: Find the longest common subsequence;

Solution:

Longest Common subsequence (LCS ):

String: S: s1s2s3s4 ...... Si, T: t1t2t3t4 ...... TJ;

Define an array DP [I] [J] to indicate s1s2s3s4 ....... si and t1t2t3t4 .......... the longest common subsequence corresponding to TJ;

Then, the longest common subsequence corresponding to s1s2s3s4 ...... Si + 1 and t1t2t3t4 ...... TJ + 1 may be:

(1) If Si + 1 = TJ + 1, s1s2s3s4 ....... si and t1t2t3t4 .......... the longest common subsequence corresponding to TJ + 1;

(2) s1s2s3s4 ...... Si + 1 and t1t2t3t4 ...... TJ correspond to the longest common subsequence;

(3) s1s2s3s4 ...... Si and t1t2t3t4 ...... The Longest Common subsequence corresponding to TJ + 1;

So the state transition equation of DP is DP [I] [J] = max (DP [I-1] [J-1] + (A [I] = B [J]), max (DP [I-1] [J], DP [I] [J-1])

#include <iostream>#include <string.h>using namespace std;char a[1000],b[1000];int dp[1000][1000];int max(int a,int b){return a>b?a:b;}int LCS(char *a,char *b){int n=strlen(a),m=strlen(b);for (int i=0;i<n;i++)dp[i][0]=0;for (int j=0;j<m;j++)dp[0][j]=0;for (int i=0;i<n;i++)for (int j=0;j<m;j++){if (a[i]==b[j])dp[i+1][j+1]=dp[i][j]+1;elsedp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);}return dp[n][m];}int main(){while (cin>>a>>b){cout<<LCS(a,b)<<endl;}return 0;} 


Poj 1458 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.