hdu 1159, LCS, dynamic programming, recursive backtrack vs iterative backtrack vs incremental, C++ 分类: hdoj 2015-07-10 04:14 112人阅读 评论(0) 收藏
thanks prof. Abhiram Ranade for his vedio on Longest Common Subsequence ‘s back track search view in lecture 19, nice explanation indeed.
// back track, recursive, 390 ms, O(m*n) memory
#include <cstdio>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#define MAXSIZE 1001
using std::string;
char *p1, *p2;
int M, N;
int table[MAXSIZE][MAXSIZE];
int solveLCSlength(int i, int j) {
if(table[i][j]>=0) return table[i][j];
int k;
for(k=j;k<N && p2[k]!=p1[i];++k) {}
if(k!=N) table[i][j]=std::max(solveLCSlength(i+1,j),1+solveLCSlength(i+1,k+1));
else table[i][j]=solveLCSlength(i+1,j);
return table[i][j];
}
int LCSlength(string &s1, string &s2) {
p1=&s1[0], p2=&s2[0], M=s1.size(), N=s2.size();
for(int i=0;i<M;++i) {
for(int j=0;j<N;++j) { table[i][j]=-1; }
table[i][N]=0;
}
for(int j=0;j<=N;++j) { table[M][j]=0; }
return solveLCSlength(0,0);
}
int main() {
string s1, s2;
while(std::cin >> s1 >> s2) {
printf("%d\n",LCSlength(s1,s2));
}
return 0;
}
// turn recursive backtrack version to iterative and reduce memory cost to O(m+n) , 312ms
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXSIZE 1001
int LCSlength(char *s1, char *s2) {
static int table[MAXSIZE<<1];
int len1,len2, i,j,k, *prev,*curr;
len1=strlen(s1), len2=strlen(s2);
prev=&table[0], curr=&table[len2+1];
for(i=0;i<=len2;++i) prev[i]=0;
curr[len2]=0;
for(i=len1-1;i>=0;--i) {
char tmp=s1[i];
for(j=len2-1;j>=0;--j) {
for(k=j;k<len2 && tmp!=s2[k];++k) {}
curr[j]=prev[j];
if(k!=len2 && curr[j]==prev[k+1]) ++curr[j];
}
std::swap(prev,curr);
}
return prev[0];
}
int main() {
char s1[MAXSIZE], s2[MAXSIZE];
while(scanf("%s%s",s1,s2)==2) {
printf("%d\n",LCSlength(s1,s2));
}
return 0;
}
// incremental, 31ms, O(m+n) memory
#include <cstdio>
#include <algorithm>
#define MAXSIZE 1001
int main() {
char s1[MAXSIZE], s2[MAXSIZE];
int table[MAXSIZE<<1], *prev, *curr;
int len1,len2, i,j;
while(scanf("%s%s",s1,s2)==2) {
len1=strlen(s1), len2=strlen(s2);
prev=table, curr=&table[len2+1];
for(i=0;i<=len2+1;++i) prev[i]=0;
for(i=1;i<=len1;++i) {
for(j=1;j<=len2;++j) {
if(s1[i-1]==s2[j-1]) curr[j]=1+prev[j-1];
else curr[j]=prev[j]>curr[j-1]?prev[j]:curr[j-1];
}
std::swap(curr,prev);
}
printf("%d\n",prev[len2]);
}
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 1159, LCS, dynamic programming, recursive backtrack vs iterative backtrack vs incremental, C++ 分类: hdoj 2015-07-10 04:14 112人阅读 评论(0) 收藏的更多相关文章
- Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- 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 ...
- hdu 1712, multiple-choice knapsack, 分类: hdoj 2015-07-18 13:25 152人阅读 评论(0) 收藏
reference: 6.4 knapsack in Algorithms(算法概论), Sanjoy Dasgupta University of California, San Diego Chr ...
- 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 de ...
- 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 ...
- A simple problem 分类: 哈希 HDU 2015-08-06 08:06 1人阅读 评论(0) 收藏
A simple problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU 2101 A + B Problem Too 分类: ACM 2015-06-16 23:57 18人阅读 评论(0) 收藏
A + B Problem Too Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- OpenCV2+入门系列(三):遍历图像的几种方法
根据OpenCV中Mat类型的结构和内存中存储方式,此处给出三种对图像进行遍历的方法.首先给出基础的读取图片代码,在中间替换三种遍历方法即可,本文中,程序将遍历图像并将所有像素点置为255,所有运行结 ...
- (转载)MongoingDB常用操作
mongo –path db.AddUser(username,password) 添加用户 db.auth(usrename,password) 设置数据库连接验证 db.cloneDat ...
- Ubuntu 14.04下搜狗输入法崩溃重启
pidof fcitx | xargs kill pidof sogou-qimpanel | xargs kill nohup fcitx >/dev/>/dev/null & ...
- Android Studio -修改LogCat的颜色
Android Studio -修改LogCat的颜色 author:Kang,Leo weibo:http://weibo.com/kangyi 效果图 设置 Preference->Edit ...
- ArcGIS影像配准与空间配准
ArcGIS影像配准与空间配准 ArcGIS影像配准与空间配准 地图配准可分为影像配准和空间配准.影像配准的对象是raster图,譬如TIFF图.配准后的图可以保存为ESRI GRID, TIFF,或 ...
- md语法之行内代码和代码片续集
md语法之行内代码和代码片 一行之内嵌入一小段代码, 简称行内代码. 其方法为: 用撇号把代码围起来. 比如: import numpy as ny就可以了. 代码片的方法: 三个连续的撇号+pyth ...
- Sublime Text 配置记录
sublime userSetting sublime theme sublime plug sublime userSetting 对sublime的配置 { "color_scheme& ...
- DataTable转换为Json字符串的三种方法
//第一种:使用StringBuilder public string DataTableToJson(DataTable table) { var JsonString = new StringB ...
- Python正则化学习
- mysql大数据分表记录app用户的坐标数据
最近提到一个需求.需要记录app用户在使用app中的移动轨迹,即坐标值.每分钟上传一次XY坐标,有点类似跑步软件的描线轨迹. 不考虑app如何获取,反正api只要接受到坐标数据 就记录下来保存到数据库 ...