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) 收藏
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) 收藏的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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) ...
- 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 ...
随机推荐
- java.lang.ExceptionInInitializerError /NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Nati ...
- 个人博客作业Week1
个人博客作业Week1 一.问题 通读<构建之法>我有一下几个问题 PM没有参与代码编如何进行管理. 软件工程师的职业资格考试对我们来说很有必要吗. 当我们为用户开发软件时我们需要了解用户 ...
- 程序设计入门——C语言 第8周编程练习 2GPS数据处理(6分)
题目内容: NMEA-0183协议是为了在不同的GPS(全球定位系统)导航设备中建立统一的BTCM(海事无线电技术委员会)标准,由美国国家海洋电子协会(NMEA-The National Marine ...
- node.js学习笔记(二)
1.接受参数:get 和 post login.html <form action="./login" method="get"><!--me ...
- 嵌入式Linux学习笔记(0)基础命令。——Arvin
学习记录: 到今天为止ARM裸机开发学习进程:1.2.1-1.2.14 预科班知识Linux介绍学习进程:0.2.1-0.2.6 学习内容笔记: 学习了Linux的开发方式的优劣介绍 学习了常用文件夹 ...
- ROS学习笔记(二)——ubantu 14.04 安装
0.采用双系统安装(U盘安装) 1.安装文件在ubantu官网下载: ubantu官网 :https://www.ubuntu.com/ ubuntu的server版和desktop版有什么区? (来 ...
- MYSQL 模糊查询
下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...
- jquery写的ajax
1.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8& ...
- Python常用内置函数总结
一.数学相关 1.绝对值:abs(-1)2.最大最小值:max([1,2,3]).min([1,2,3])3.序列长度:len('abc').len([1,2,3]).len((1,2,3))4.取模 ...
- linux 挂载光盘:mount: you must specify the filesystem type
尝试挂载光盘镜像时出现mount: you must specify the filesystem type 使用-t auto -t iso9660 或不加参数都搞不定,最后在以下链接找到解决办法: ...