最简单的动态规划
#include <stdio.h>
#include <string.h>
char buf1[1005];
char buf2[1005];
int dp[1005][1005];
int _max(int a, int b){
return a>b ? a:b;
}
void func(){
int len1, len2, i, j;
len1 = strlen(buf1+1); len2 = strlen(buf2+1);
for(i=1; i<=len1; i++){
for(j=1; j<=len2; j++){
if(buf1[i] == buf2[j]){
dp[i][j] = dp[i-1][j-1] + 1;
}
else{
dp[i][j] = _max(dp[i-1][j], dp[i][j-1]);
}
}
}
printf("%d\n", dp[len1][len2]);
}
int main(void){
//freopen("input.dat", "r", stdin);
while(gets(buf1+1) && gets(buf2+1)){
func();
}
return 0;
}