Longest Common subsequence (DP) and longest sequence dp

Source: Internet
Author: User

Longest Common subsequence (DP) and longest sequence dp
Description

The subsequence of a given sequence is obtained after several elements are deleted from the sequence. To be exact, if a sequence is specifiedX= {X1,X2,...,Xm}, Then another sequenceZ= {Z1,Z2,...,Zk},XThe subsequence {I1,I2,...,Ik}, So that for allJ= 1, 2 ,...,K, YesZj=Xij

Returns two character sequences.XAndYTo obtain their longest common subsequences.

Input

The number of input first behavior test samples T, followed by T test samples. The first line of each test sample is a stringXThe second line is a string.Y.XAndYIt only contains uppercase letters and cannot exceed 1000 characters in length.

Output

Each test sample outputs a line with only one integer representing a string.XAnd stringYThe length of the longest common subsequence.

Sample Input

2
ABCDE
ACE
AAABBBCCC
AABBCC

Sample Output

3
6

#include<iostream>#include<stdio.h>#include<string.h>using namespace std;char x[1001];char y[1002];int c[1001][1001];void length(int x_len,int y_len){    for(int i=1;i<=x_len;i++){        for(int j=1;j<=y_len;j++){            if(x[i-1]==y[j-1]){                c[i][j]=c[i-1][j-1]+1;            }else c[i][j]=max(c[i-1][j],c[i][j-1]);        }    }    cout << c[x_len][y_len] << endl;}int main(){    int T;    cin >> T;    for(int i=1;i<=T;i++){        scanf("%s%s",x,y);        length(strlen(x),strlen(y));    }}

 

Related Article

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.