【codeup墓地】1132: 最长公共子序列(动态规划)

版权声明:假装有个原创声明……虽然少许博文不属于完全原创,但也是自己辛辛苦苦总结的,转载请注明出处,感谢! https://blog.csdn.net/m0_37454852/article/details/88533298

原题链接

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
using namespace std;

const int MAX = 110, INF = 1<<30;
char str1[MAX], str2[MAX];
int dp[MAX][MAX] = {0};


int main()
{
    while(scanf("%s %s", str1, str2) != EOF)
    {
        int max_len = 0;
        int len1 = strlen(str1);
        int len2 = strlen(str2);
        for(int i=1; i<=len1; i++)
        {
            for(int j=1; j<=len2; j++)
            {
                if(str1[i-1] == str2[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
                else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                max_len = max(max_len, dp[i][j]);
            }
        }
        printf("%d\n", max_len);
    }
	return 0;
}



猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/88533298
今日推荐