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)); }}