一本通 1265:【例9.9】最长公共子序列

最长公共子序列

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
//Mystery_Sky
//
#define M 1000
string s1, s2;
int len1, len2;
int f[M][M], ans;
int main() {
	cin >> s1;
	cin >> s2;
	len1 = s1.size(), len2 = s2.size();
	for(int i = 1; i <= len1; i++)
		for(int j = 1; j <= len2; j++) {
			if(s1[i-1] == s2[j-1]) 
				f[i][j] = max(f[i][j], f[i-1][j-1] + 1);
			else f[i][j] = max(f[i][j-1], f[i-1][j]);
		}
	printf("%d\n", f[len1][len2]);
	return 0;
}
posted @ 2019-05-09 20:51  Mystery_Sky  阅读(526)  评论(0编辑  收藏  举报