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. es6还欠完善的地方

    const的可变性 const用于声明常量. 什么是常量,声明后的值不可更改. 对于值类型,比如string,number等等.const声明确实有效. const str = "strin ...

  2. mysql安装,配置。

    看到百度经验上有一篇文章比较好,后来发现是舍友写的,他同意后,便复制到我的博客园中,希望更多小白看到,原地址:http://jingyan.baidu.com/article/597035521d5d ...

  3. Redis教程(三) list类型

     一.概述: redis的list类型其实就是一个每个子元素都是string类型的双向链表.所以[lr]push和[lr]pop命令的算法时间复杂度都是O(1) 另外list会记录链表的长度.所以ll ...

  4. mysql delete 使用别名 语法

    今天删除数据,写了这么条sql语句, DELETE   from  sys_menus s WHERE s.MENU_ID in (86,87,88); 结果报错.. [Err] 1064 - You ...

  5. ssh 登录

    一.ssh登录过程 在实际开发中,经常使用ssh进行远程登录.ssh 登录到远程主机的过程包括: 版本号协商 密钥和算法协商 认证 交互 1.1 版本号协商阶段 (1) 服务端打开22端口(也可以为了 ...

  6. Session的使用过程中应注意的一个小问题

    在学习AllEmpty大神的从零开始编写自己的C#框架系列文章中,发现的问题:在验证码的缓存Session["vcode"]的赋值时,发现Session["vcode&q ...

  7. Linq常用语法详细

    1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; // ...

  8. SAP 设置屏幕字段的隐藏、显示、必填和可选,以设置物料组为例

    1.事务码MM01,把物料组设为选填字段. 2.找到物料组的屏幕字段. 3.在后台根据屏幕字段找到对应字段组.后台路径:后勤-常规—物料主数据—字段选择—给字段组分配字段.点击后面的箭头进入下一屏幕. ...

  9. Android 防止OOM优化

    1. Android2.x及以下的系统优化: Bitmap被解码后的像素被存储在Native Heap中, Dalvik Heap有个external计数,记录了Bitmap所占用的内存. 当 Dal ...

  10. Android开发之无线遥控器

    最近弄了一个UDP/TCP的小东西,主要需要实现的功能如下(服务器端): 1.基于局域网 2.服务器端网络接口为无线与有线 3.服务器端接收到客户端的数据需要模拟按键进行处理 4.开机自启动 5.使用 ...