博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1458---(最长公共子序列最基础题)
阅读量:4046 次
发布时间:2019-05-25

本文共 782 字,大约阅读时间需要 2 分钟。

最长公共子序列(LCS)

dp[i][j]:字符串s1…si和字符串t1…tj对应的LCS长度。
递推关系:
dp[i][j]=dp[i-1][j-1]+1; (s[i-1]=t[i-1])//字符串从0开始存放。
dp[i][j]=max(dp[i-1][j],dp[i][j-1]); (其他)
边界控制:
dp[i][j]=0(i=0||j=0) //意味着至少有一个字符串为空。

#include
#include
#include
#include
using namespace std;char s[1000], t[1000];int dp[1000][1000];int main(){ while (cin >> s >>t ) { int ls = strlen(s); int lt = strlen(t); memset(dp, 0, sizeof(dp)); for (int i = 1; i <= ls; i++) for (int j = 1; j <= lt; j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); } cout << dp[ls][lt] << endl; } system("pause");}

转载地址:http://nkyci.baihongyu.com/

你可能感兴趣的文章
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>