最长公共子序列
举个例子,cnblogs这个字符串中子序列有多少个呢?很显然有27个,比如其中的cb,cgs等等都是其子序列,我们可以看出
子序列不见得一定是连续的,连续的那是子串。
从图中我们看到了最长公共子序列为blog,仔细想想我们可以发现其实最长公共子序列的个数不是唯一的,可能会有两个以上,
但是长度一定是唯一的,比如这里的最长公共子序列的长度为4。
//动态规划
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int dp[101][101];
int main(){
int length1,length2;
char array1[102];
char array2[102];
gets(array1+1);
gets(array2+1);
length1=strlen(array1+1);
length2=strlen(array2+1);
memset(dp,0,sizeof(dp));
for(int i=1;i<=length1;i++){
for(int j=1;j<=length2;j++){
if(array1[i]==array2[j])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=dp[i-1][j]>dp[i][j-1]?dp[i-1][j]:dp[i][j-1];
}
}
cout<<dp[length1][length2]<<'\n';
return 0;
}