[算法] doj 1605 Common Subsequence 公共子序列

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;
const int N = 505;
char s1[N], s2[N];
int dp[N][N];
void solve() {
	int len1 = strlen(s1+1);
	int len2 = strlen(s2+1);
	memset(dp, 0, sizeof(dp));
	for(int i = 0; i <= len2; i++) {
		dp[0][i] = 0;
	}
	for(int i = 0; i <= len1; ++i) {
		dp[i][0] = 0;
	}
	for(int i = 1; i <= len1; ++i) {
		for(int j = 1; j <= len2; ++j) {
			if(s1[i] == s2[j]) {
				dp[i][j] = dp[i-1][j-1] + 1;
			}
			else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
		}
	}
	cout << dp[len1][len2] << endl;
}
int main() {
	while(scanf("%s%s", s1+1, s2+1) == 2) {
		solve();
	}
	return 0;
}

posted @ 2013-04-09 14:34  小尼人00  阅读(175)  评论(0编辑  收藏  举报