同:ZJU 1733,HDU 1159
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int dp[1010][1010]; int main() { char a[1010], b[1010]; int la, lb; while(scanf("%s%s", a, b) != EOF) { memset(dp, 0, sizeof(dp)); la = strlen(a), lb = strlen(b); for(int i = 0; i < la; i++) { for(int j = 0; j < lb; j++) { if(a[i] == b[j]) { dp[i+1][j+1] = dp[i][j] + 1; } else { dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]); } } } printf("%d ", dp[la][lb]); } return 0; }