hdu 1159, LCS, dynamic programming, recursive backtrack vs incremental, C++

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;
}

// 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 01:29:55

hdu 1159, LCS, dynamic programming, recursive backtrack vs incremental, C++的相关文章

HDU 1159 LCS最长公共子序列

1 #include <cstdio> 2 #include <cstring> 3 4 using namespace std; 5 const int N = 1005; 6 #define max(a,b) a>b?a:b 7 8 char a[N] , b[N]; 9 int dp[N][N]; 10 11 int main() 12 { 13 while(scanf("%s%s" , a+1 , b+1) != EOF){ 14 int l1 =

HDU 4972 A simple dynamic programming problem(推理)

HDU 4972 A simple dynamic programming problem 题目链接 推理,会发现只有前一个和当前一个分数为(1, 2)或(2, 1)的时候,会有两种加分方法,其他情况最多就一种情况,所以只要统计(1, 2),(2, 1)的个数,最后判断分差是否为0,如果不为0,那么可能是正或负,那就是两倍 代码: #include <cstdio> #include <cstring> const int N = 100005; int t, n, a[N]; i

hdu 4972 A simple dynamic programming problem(高效)

题目链接:hdu 4972 A simple dynamic programming problem 题目大意:两支球队进行篮球比赛,每进一次球后更新比分牌,比分牌的计数方法是记录两队比分差的绝对值,每次进球的分可能是1,2,3分.给定比赛中的计分情况,问说最后比分有多少种情况. 解题思路:分类讨论: 相邻计分为1-2或者2-1的时候,会对应有两种的的分情况 相邻计分之差大于3或者说相等并且不等于1的话,为非法输入 其他情况下,不会造成新的比分情况产生 对于最后一次比分差为0的情况,就没有谁赢谁

hdu 4972 A simple dynamic programming problem (转化 乱搞 思维题) 2014多校10

题目链接 题意:给定一个数组记录两队之间分差,只记分差,不记谁高谁低,问最终有多少种比分的可能性 分析: 类似cf的题目,比赛的时候都没想出来,简直笨到极点..... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <vector> 7 #include &

hdu 4223 Dynamic Programming?

Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solving complex problems by breaking them down

hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup

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][

Dynamic Programming | Set 4 (Longest Common Subsequence)

首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", "bdf", "aeg", '"acefg"等都是"abcdefg"的子序列.因此,一个长度为n的序列拥有2^n中可能的子序列(序列中的每一个元素只有选或者不选两种可能,因此是2^n). Example: LCS for inp

HDU 1159——Common Subsequence(DP)

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23279    Accepted Submission(s): 10242 Problem Description A subsequence of a given sequence is the given sequence with some e

Dynamic Programming

We began our study of algorithmic techniques with greedy algorithms, which in some sense form the most natural approach to algorithm design. Faced with a new computational problem, we've seen that it's not hard to propose multiple possible greedy alg