暑期dp46道(43)--HDOJ 1159 最长公共子序列(可不连续)

来源:互联网 发布:别墅网络覆盖方案 编辑:程序博客网 时间:2024/06/05 17:38

题目链接:HDOJ 1159



简单dp,裸题,直接上代码:

#include<cstdio>#include<cstring>#include<string>#define debug 0#define M(a) memset(a,0,sizeof(a))#define Max(a,b) ((a>b)?a:b)const int maxn = 1000 + 5;int dp[maxn][maxn];char str1[maxn], str2[maxn];void Do(){int ans = 0;for (int i = 1; i <= strlen(str1); i++)for (int j = 1; j <= strlen(str2); j++){if (i == 1 && j == 1)dp[i][j] = (str1[i - 1] == str2[j - 1]) ? 1 : 0;//dp[1][1]最好判断else   if (str1[i - 1] == str2[j - 1]){dp[i][j] = dp[i - 1][j - 1] + 1;//当前最长公共子序列长度}elsedp[i][j] = Max(dp[i - 1][j], dp[i][j - 1]);ans = Max(ans, dp[i][j]);}printf("%d\n", ans);}int main(){#if debugfreopen("in.txt", "r", stdin);#endif//debugwhile (~scanf("%s%s", str1, str2)){//puts(str1);//puts(str2);M(dp);//dp[][]初始化Do();}return 0;}


1 0