51nod 1006 最长公共子序列Lcs(基础dp)
来源:互联网 发布:java线程工作原理 编辑:程序博客网 时间:2024/06/05 17:45
设两个字符串为str1和str2,dp[i][j]表示str1[i]和str2[j]的最长公共子序列,如果str1[i]==str2[j],dp[i][j]=dp[i-1][j-1]+1,如果str1[i]!=str2[j],dp[i][j]=max(dp[i-1][j],dp[i][j-1]),记录公共子序列的时候,和dp的过程差不多
测试案例的dp数组:
输出路径的过程结合dp数组和代码一看就懂了 a b d k s c a ba 1 1 1 1 1 1 1 1 b 1 2 2 2 2 2 2 2 c 1 2 2 2 2 3 3 3 i 1 2 2 2 2 3 3 3 c 1 2 2 2 2 3 3 3 b 1 2 2 2 2 3 3 4 a 1 2 2 2 2 3 4 4
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 1010;char str1[MAXN];char str2[MAXN];int dp[MAXN][MAXN];char path[MAXN];int plen;void lcs(int len1, int len2){ for(int i = 0; i < len1; ++i) { for(int j = 0; j < len2; ++j) { if(str1[i] == str2[j]) dp[i+1][j+1] = dp[i][j]+1; else dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1]); } }}int main(){ //freopen("out","w",stdout); scanf(" %s %s",str1,str2); int len1 = strlen(str1); int len2 = strlen(str2); lcs(len1,len2); int i = len1-1; int j = len2-1; plen = 0; //记录路径 while(i >= 0 && j >= 0) { if(str1[i] == str2[j]) { path[plen++] = str1[i]; --i; --j; } else if(dp[i+1][j] >= dp[i][j+1])//说明lcs是str1[0~i]和str2[0~j-1]的lcs --j; else --i; } for(int i = plen-1; i >= 0; --i) printf("%c",path[i]); printf("\n"); return 0;}
0 0
- 51Nod 1006 最长公共子序列Lcs(dp)
- 51Nod 1006 最长公共子序列Lcs(DP)
- 51nod 1006 最长公共子序列Lcs(基础dp)
- 51Nod 1006 最长公共子序列LCS DP水题
- 【51Nod】1006 - 最长公共子序列Lcs(LCS)
- 最长公共子序列(Lcs)51NOD-1006
- 51nod 1006 最长公共子序列Lcs【LCS】
- lcs 51nod 1006 最长公共子序列Lcs
- 【dp基础课程】矩阵取数问题+最大子段和+最长公共子序列(LCS)【51nod】
- 最长公共子序列Lcs 51Nod
- 最长公共子序列Lcs---51--Nod
- 51nod 1006 1006 最长公共子序列Lcs
- 51nod oj 1006 最长公共子序列【dp+前向记录求Lcs】
- 51nod 1006:最长公共子序列Lcs
- 51Nod-1006-最长公共子序列Lcs
- [51nod]1006 最长公共子序列Lcs
- 51Nod 1006 - 最长公共子序列(Lcs)
- 51nod 1006 最长公共子序列Lcs
- 数据结构之--图的讲解与C语言实现
- java多线程之内存可见性
- 使用maven的一些心得记录
- Corn表达式
- ImageNet Classification with Deep Convolutional Neural Networks(译文)
- 51nod 1006 最长公共子序列Lcs(基础dp)
- Ubuntu16.04 display setting (xfce4-display-settings) segmentation fault 闪退
- BZOJ 1257 同除等价类 + 等比数列
- Eclipse下新建一个tld文件
- 安装Scipy
- 打印杨辉三角
- Retinex图像增强算法(SSR, MSR, MSRCR)详解及其OpenCV源码
- hdu 6024 Building Shops 【递归+记忆存储】
- 利用puppet实现自动化运维