当前位置: 首页> 汽车> 时评 > 百姓网免费发布信息网下载_大型的营销型网站_优化资源配置_网址关键词查询网站

百姓网免费发布信息网下载_大型的营销型网站_优化资源配置_网址关键词查询网站

时间:2025/7/11 0:43:15来源:https://blog.csdn.net/weixin_63145113/article/details/146335368 浏览次数: 1次
百姓网免费发布信息网下载_大型的营销型网站_优化资源配置_网址关键词查询网站

代码如下:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>int maxleng(char* a, char* b, int m, int n)//求最长公共子序列的长度
{int** dp = (int**)malloc(sizeof(int*) * (m + 1));for (int i = 0; i <= m; i++){dp[i] = (int*)malloc(sizeof(int) * (n + 1));}for (int i = 0; i <= m; i++){for (int j = 0; j <= n; j++){if (i == 0 || j == 0){dp[i][j] = 0;}else if (a[i - 1] == b[j - 1]){dp[i][j] = dp[i - 1][j - 1] + 1;}else{dp[i][j] = (dp[i - 1][j] > dp[i][j - 1] ? dp[i - 1][j] : dp[i][j - 1]);}}}int result = dp[m][n];for (int i = 0; i <= m; i++){free(dp[i]);}free(dp);return result;
}void printchar(char* a, char* b, int m, int n)//输出最长公共子序列
{int** dp = (int**)malloc(sizeof(int*) * (m + 1));for (int i = 0; i <= m; i++){dp[i] = (int*)malloc(sizeof(int) * (n + 1));}maxleng(a, b, m, n);int index = dp[m][n];char* lcs_str = (char*)malloc(sizeof(char) * (index + 1));if (lcs_str == NULL){printf("内存分配失败\n");return;}lcs_str[index] = '\0';int i = m, j = n;while (i > 0 && j > 0){if (a[i - 1] == b[j - 1]){lcs_str[--index] = a[i - 1];i--;j--;}else if (dp[i - 1][j] > dp[i][j - 1]){i--;}elsej--;}printf("最长公共子序列是: %s\n", lcs_str);for (int i = 0; i <= m; i++){free(dp[i]);}free(dp);free(lcs_str);
}int main()
{char a[] = "elo world!";char b[] = "welcome odl!";int m = strlen(a);int n = strlen(b);int result = maxleng(a, b, m, n);printf("最长公共子序列的长度是: %d\n", result);printchar(a, b, m, n);return 0;
}

关键字:百姓网免费发布信息网下载_大型的营销型网站_优化资源配置_网址关键词查询网站

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: