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) 收藏的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. .net 网站开发学习资源

    慕课网 前端基础学习 http://www.imooc.com/course/list?c=fe 了解需求 例子之一 http://wenku.it168.com/d_000517899.shtml ...

  2. gif图片加载问题

    之前从没想过,gif图片在网页中显示还会有问题,不过今天算是遇到了bug,问题是在vc嵌入的网页里面,有三个需要显示加载的动态图,刚开始在html中写好,在div向上滚动显示gif的时候就成了静态的. ...

  3. rxjava源码中的线程知识

    rxjava源码中的线程知识 rx的最精简的总结就是:异步 这里说一下以下的五个类 1.Future2.ConcurrentLinkedQueue3.volatile关键字4.AtomicRefere ...

  4. 2014最后一天,好烦!这个问题从来没遇到过!网上查找了很多办法都没解决!并且no wifi 了!

    org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread ...

  5. Treap

    treap模板 期望复杂度为O(nlogn) 带合并的treap期望复杂度为O(nlognlogn) #include <bits/stdc++.h> ; struct tree{ int ...

  6. Java中的JDK动态代理

    所谓代理,其实就是相当于一个中间人,当客户端需要服务端的服务时,不是客户直接去找服务,而是客户先去找代理,告诉代理需要什么服务,然后代理再去服务端找服务,最后将结果返回给客户. 在日常生活中,就拿买火 ...

  7. 根据条件动态拼接LinQ的where条件字串

    var items1 = from c in customer == ? c.FirstName == == ? c.LastName == "BBB" : true) selec ...

  8. h5

    1. 在iPhone 手机上默认值是(电话号码显示为拨号的超链接): <meta name="format-detection" content="telephon ...

  9. [sqoop1.99.6] 基于1.99.6版本的一个小例子

    1.创建mysql数据库.表.以及测试数据mysql> desc test;+-------+-------------+------+-----+---------+------------- ...

  10. (转载)数据库出现ORA-00283/ORA-01610的问题

    在这里需要感谢棉花糖给予无私帮助,真的谢谢他!http://blog.itpub.net/67668/viewspace-353270/处理过程可以参照http://www.itpub.net/vie ...