51Nod 1006 最长公共子序列LCS DP水题
来源:互联网 发布:如何联系淘宝达人 编辑:程序博客网 时间:2024/06/04 08:05
题目链接:51 Nod 1006
题意: 求A,B的最长公共子序列并输出。
分析:先求出最长公共子序列的长度,然后再根据最长公共子序列的长度逆序求出最长公共子序列。
#include <cmath>#include <queue>#include <vector>#include <cstdio>#include <string>#include <cstring>#include <iomanip>#include <iostream>#include <algorithm>using namespace std;//#pragma comment(linker, "/STACK:1024000000,1024000000")#define FIN freopen("input.txt","r",stdin)#define FOUT freopen("output.txt","w",stdout)#define fst first#define snd second#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1typedef __int64 LL;//typedef long long LL;typedef unsigned int uint;typedef pair<int, int> PII;typedef pair<LL, LL> PLL;const int INF = 0x3f3f3f3f;const double eps = 1e-6;const int MAXN = 1000 + 5;const int MAXM = 600 + 5;char A[MAXN], B[MAXN];int dp[MAXN][MAXN];char R[MAXN];int main() {#ifndef ONLINE_JUDGE FIN;#endif // ONLINE_JUDGE while (~scanf("%s %s", A, B)) { memset(dp, 0, sizeof(dp)); int lenA = strlen(A); int lenB = strlen(B); for (int i = 0; i < lenA; i++) { for (int j = 0; j < lenB; j++) { if (A[i] == B[j]) { if (i == 0 || j == 0) dp[i][j] = 1; else dp[i][j] = dp[i - 1][j - 1] + 1; } else { if (i == 0 && j == 0) dp[i][j] = 0; else if (i == 0) dp[i][j] = dp[i][j - 1]; else if (j == 0) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } // printf("%d\n", dp[lenA - 1][lenB - 1]); int temp = dp[lenA - 1][lenB - 1]; R[temp] = '\0'; for (int i = lenA - 1; i >= 0; i--) { if (!temp) break; for (int j = lenB - 1; j >= 0; j--) { if (A[i] == B[j] && dp[i][j] == temp) { R[--temp] = A[i]; break; } } } puts(R); } return 0;}
0 0
- 51Nod 1006 最长公共子序列LCS DP水题
- 51Nod 1006 最长公共子序列Lcs(dp)
- 51Nod 1006 最长公共子序列Lcs(DP)
- 【51Nod】1006 - 最长公共子序列Lcs(LCS)
- 51nod 1006 最长公共子序列Lcs【LCS】
- lcs 51nod 1006 最长公共子序列Lcs
- 最长公共子序列Lcs 51Nod
- 最长公共子序列Lcs---51--Nod
- 51nod 1006 1006 最长公共子序列Lcs
- 51nod oj 1006 最长公共子序列【dp+前向记录求Lcs】
- 51nod 1006 最长公共子序列Lcs(基础dp)
- 51nod 1006:最长公共子序列Lcs
- 51Nod-1006-最长公共子序列Lcs
- [51nod]1006 最长公共子序列Lcs
- 51Nod 1006 - 最长公共子序列(Lcs)
- 51nod 1006 最长公共子序列Lcs
- 51nod 1006 最长公共子序列Lcs
- 51Nod 1006 最长公共子序列Lcs
- android Universal-Image-Loader框架学习
- 在mac上安装scikit-learn
- javaScript--05 DOM基础 12.8
- [HTML] 图片标签、绝对路径 和 相对路径
- 利用SharePoint 2013 创建Service Desk 网站之一线工程师处理问题界面!
- 51Nod 1006 最长公共子序列LCS DP水题
- 004——session的持久化生命周期
- SQL 模糊查询 和 escape
- seaJs
- 变形的杨辉三角形--华为
- Cloudera 5.7集群部署(二)--CM、CDH安装
- C#反射
- ffmpeg常用命令记录
- 位操作训练