DP
1、确定状态 maxLen[i][j]表示 a的左侧i个字符与b的左侧j个字符最长子序列长度
2、边界条件(初始)maxLen[i][0] = maxLen[0][j] = 0;
3、状态转移
4、复杂度 = 状态数*计算每个状态所需时间
#include<stdio.h>
#include<string.h>
#define MAX(a,b) ((a) > (b) ? (a):(b))
char a[1001],b[10001];
int maxLen[1001][1001];
int main()
{
while(scanf("%s%s",a,b) != EOF){
int len1 = strlen(a);
int len2 = strlen(b);
int len = MAX(len1,len2);
//printf("%d",len);
for(int i = 0; i < len; ++i){
maxLen[i][0] = 0;
maxLen[0][i] = 0;
}
for(int i = 1; i <= len1; ++i){
for(int j = 1; j <= len2; ++j){
if(a[i-1] == b[j-1]){
maxLen[i][j] = maxLen[i-1][j-1] + 1;
}
else{
maxLen[i][j] = MAX(maxLen[i-1][j],maxLen[i][j-1]);
}
}
}
printf("%d\n",maxLen[len1][len2]);
}
return 0;
}