a typical variant of LCS algo.

the key point here is, the dp[][] array contains enough message to determine the LCS, not only the length, but all of LCS candidate, we can backtrack to find all of LCS.

for backtrack, one criteria is

dp[i-1][j]==dp[i][j]-1 && dp[i][j-1]==dp[i][j]-1

another is

dp[i][j]==dp[i-1][j-1]+1 && str1[i]==str2[j]

both is ok, here the first one is used.

//

#include <cstdio>
#include <cstring>
#include <algorithm> struct myNode{ int x,y; }; #define MAXSIZE 105
int dp[MAXSIZE][MAXSIZE];
myNode pos[MAXSIZE];
char str1[MAXSIZE], str2[MAXSIZE]; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int i,j,k,ch,len1,len2,len3;
while(scanf("%s%s",str1+1, str2+1)==2) {
len1=strlen(str1+1), len2=strlen(str2+1);
for(i=1;i<=len1;++i) {
for(ch=str1[i], j=1;j<=len2;++j) {
if(ch==str2[j]) dp[i][j]=1+dp[i-1][j-1];
else dp[i][j]=std::max(dp[i][j-1],dp[i-1][j]);
}
}
for(i=len1, j=len2, len3=dp[len1][len2]-1;len3>=0;--len3) {
while(1) {
for(k=j;len3!=dp[i][k-1];--k) {}
if(len3==dp[i-1][k]) {
pos[len3]={i,k};
--i, j=k-1;
break;
}
else {
--i;k=j;
}
}
}
len3=dp[len1][len2];
pos[len3]={len1+1,len2};
for( i=1, j=1, k=0;k<=len3;++k,++i,++j) {
for(;i<pos[k].x;++i) putchar(str1[i]);
for(;j<pos[k].y;++j) putchar(str2[j]);
putchar(str2[j]);
}
putchar('\n');
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.

hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏的更多相关文章

  1. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  3. hdu 1052 (greedy algorithm) 分类: hdoj 2015-06-18 16:49 35人阅读 评论(0) 收藏

    thanks to http://acm.hdu.edu.cn/discuss/problem/post/reply.php?action=support&postid=19638&m ...

  4. hdu1171 Big Event in HDU(01背包) 2016-05-28 16:32 75人阅读 评论(0) 收藏

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. hdu 1082, stack emulation, and how to remove redundancy 分类: hdoj 2015-07-16 02:24 86人阅读 评论(0) 收藏

    use fgets, and remove the potential '\n' in the string's last postion. (main point) remove redundanc ...

  6. hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++ 分类: hdoj 2015-07-12 03:24 87人阅读 评论(0) 收藏

    the algorithm of three version below is essentially the same, namely, Kadane's algorithm, which is o ...

  7. Hiking 分类: 比赛 HDU 函数 2015-08-09 21:24 3人阅读 评论(0) 收藏

    Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  8. Improving the GPA 分类: 贪心 HDU 比赛 2015-08-08 16:12 11人阅读 评论(0) 收藏

    Improving the GPA Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...

  9. Task schedule 分类: 比赛 HDU 查找 2015-08-08 16:00 2人阅读 评论(0) 收藏

    Task schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. TFS二次开发系列:六、TFS的版本控制

    在TFS中对于版本控制是在WorkSpace工作区来控制的. 首先我们先整理WorkSpace的一些基本使用方法. CheckIn:迁入挂起的操作 CreateMapping:创建一个本地映射地址 D ...

  2. C# 文件及文件夹深度复制

    完善了下 文件中的文件及文件夹中的复制!如果有更优解决方案请不吝指教 protected void FileDepthCopy(string source, string target){ if (D ...

  3. HTTP协议的报文结构

    HTTP 有两类报文: (1) 请求报文----从客户向服务器发送请求报文,见图6-12(a). (2) 响应报文----从服务器到客户的回答,见图6-12(b). 由于 HTTP是面向文本的(tex ...

  4. js实现对移动设备的检测

    <script type="text/javascript"> if (browserRedirect()) { location.href = 'http:/phon ...

  5. AOP 面向切面编程

    AOP http://blog.csdn.net/xiang_j2ee/article/details/6851963 Android 支持 AspectJ 这个库来实现面向切面编程. 使用 Apac ...

  6. Spring 配置解析之Properties

    http://www.cnblogs.com/dragonfei/archive/2016/10/09/5906474.html *********************************** ...

  7. 浅谈 原生javaScript&&react 实现全局触摸按钮(附带对addeventlistener的了解)

    1.采用原生javaACript 实现全局触摸按钮 首先在控制台输出,观察事件有哪些关于触摸的字段可以使用,然后拿这些字段的数据开始来写方法. 因为要做的是全局触摸按钮,我需要拿到的是按钮时时的坐标位 ...

  8. windows插件框架

    X3C,张云贵,http://blog.csdn.net/clever101/article/details/8656720

  9. 挂载windows共享文件夹

    sudo mount -o username=用户名,password=密码 //本机IP/共享目录 ~/挂载目录

  10. WinForm 窗体基本属性、公共控件

    一.WinForm:客户端程序制作 - C/S (B/S:服务器端) 它是基于.NET Framework框架上运行,不是必须在windows系统上才能运行---------------------- ...