leetcode 1143.最长公共子序列(longest common subsequence)C语言

leetcode 1143.最长公共子序列(longest common subsequence)C语言

1.description

https://leetcode-cn.com/problems/longest-common-subsequence/description/

给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。

一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任符)后组成的新字符串。
例如,“ace” 是 “abcde” 的子序列,但 “aec” 不是 “abcde”
的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列。

若这两个字符串没有公共子序列,则返回 0。

示例 1:

输入:text1 = “abcde”, text2 = “ace”
输出:3
解释:最长公共子序列是 “ace”,它的长度为 3。

示例 2:

输入:text1 = “abc”, text2 = “abc”
输出:3
解释:最长公共子序列是 “abc”,它的长度为 3。

示例 3:

输入:text1 = “abc”, text2 = “def”
输出:0
解释:两个字符串没有公共子序列,返回 0。

提示:

1 <= text1.length <= 1000
1 <= text2.length <= 1000
输入的字符串只含有小写英文字符。

2.solution

二维dp

#define MAX(a, b) ({\
    int _a = (a); \
    int _b = (b); \
    _a >= b ? _a : _b;})

int longestCommonSubsequence(char * text1, char * text2){
    int m = strlen(text1);
    int n = strlen(text2);

    int **dp = (int**)malloc((m+1)*sizeof(int*));

    for(int i=0; i<m+1; ++i){
        dp[i] = (int*)malloc((n+1)*sizeof(int));
    }

    // 初始化第0行
    for(int i=0; i<n+1; ++i){
        dp[0][i] = 0;
    }
    // 初始化第0列
    for(int i=0; i<m+1; ++i){
        dp[i][0] = 0;
    }

    for(int i=1; i<m+1; ++i){
        for(int j=1; j<n+1; ++j){
            if(text1[i-1] == text2[j-1]){
                dp[i][j] = dp[i-1][j-1] + 1;
            }else{
                dp[i][j] = MAX(dp[i][j-1], dp[i-1][j]);
            }
        }
    }

    int res = dp[m][n];

    // free
    for(int i=0; i<m+1; ++i){
        if(dp[i]){
            free(dp[i]);
        }
    }

    if(dp){
        free(dp);
    }

    return res;
}

dp压缩到两个vector

#define MAX(a, b) ({\
    int _a = (a); \
    int _b = (b); \
    _a >= b ? _a : _b;})

static inline void swap(int **p, int **q){
    int *tmp = *p;
    *p = *q;
    *q = tmp;
}

int longestCommonSubsequence(char * text1, char * text2){
    int m = strlen(text1);
    int n = strlen(text2);

    int *pre = (int*)malloc((n+1)*sizeof(int));
    int *cur = (int*)malloc((n+1)*sizeof(int));

    for(int i=0; i<n+1; ++i){
        pre[i] = 0;
    }

    cur[0] = 0;

    for(int i=1; i<m+1; ++i){
        for(int j=1; j<n+1; ++j){
            if(text1[i-1] == text2[j-1]){
                cur[j] = pre[j-1] + 1;
            }else{
                cur[j] = MAX(cur[j-1], pre[j]);
            }
        }
        swap(&pre, &cur);
    }

    int res = pre[n];

    // free
    if(pre){
        free(pre);
    }

    if(cur){
        free(cur);
    }

    return res;
}

进一步压缩到一个vector,外加两个变量,用来存储dp[i-1] [j-1]

#define MAX(a, b) ({\
    int _a = (a); \
    int _b = (b); \
    _a >= b ? _a : _b;})

int longestCommonSubsequence(char * text1, char * text2){
    int m = strlen(text1);
    int n = strlen(text2);

    int *dp = (int*)malloc((n+1)*sizeof(int));

    for(int i=0; i<n+1; ++i){
        dp[i] = 0;
    }

    int pre = 0, next = 0;
    for(int i=1; i<m+1; ++i){
        pre = dp[0];
        for(int j=1; j<n+1; ++j){
            next = dp[j];
            if(text1[i-1] == text2[j-1]){
                dp[j] = pre + 1;
            }else{
                dp[j] = MAX(dp[j-1], dp[j]);
            }
            pre = next;
        }
    }

    int res = dp[n];

    // free
    if(dp){
        free(dp);
    }

    return res;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值