Jackson.tmm

只有奮鬥才能改變命運


  • Home

  • About

  • Posts

  • Leetcodes

  • Notes

  • Review

  • Projects

  • achievement

  • resources

  • Search

Longest Common SubString(最長公共子序列)

時間: 2022-06-14   |   分類: leetcode template   leetcode note   coding skill   | 字數: 263 字 | 閱讀: 1分鐘 | 閱讀次數:

Introduction

什麼是最長公共子序列?

給定2個字串string A和string B,2個字串中所共同擁有的最長的子字串。
例如:

String A : leetcode
String B : ecbod
他們的最長公共子序列便是`ecod`

解釋:
String A 包含了 __e_cod_ => ecod
String B 包含了 ec_od => ecod

要怎麼找到最長公共子序列LCM呢?

我們需要定義一個數組用於保存當前情況下的最優解,也就是使用DP的方式。我們需要以每個字符最為考量,並一一匹配,最後得出整體最優解。 LCM

LCM Code Template

int longestCommonSubStr(string &s1, string &s2){
        int n = s1.length();
        int m = s2.length();

        vector<vector<int>> dp(n+1,vector<int>(m+1,0));

        for(int i = 1;i<=n;i++){
            for(int j = 1;j<=m;j++){
                if(s1[i-1] == s2[j-1])
                    dp[i][j] = 1 + dp[i-1][j-1]; 
                    //the LCM of previous size + current matched
                else
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 
                    //strA[0..i-2] str[0...j-1] or strA[0..i-1] str[0...j-2] check which one have the longest LCM
            }
        }

        return dp[n][m];
    }

參考資料

经典动态规划:最长公共子序列

#dynamic programming#

文章聲明:Longest Common SubString(最長公共子序列)

文章連接:https://ryantokmanmokmtm.github.io/notes/lcm/

作者:jackson.tmm

文章聲明: 本博客文章除特別聲明外,均采用 CC BY-NC-SA 3.0許可協議,轉載請注明出處!

Palindromic String(迴文字串)
jackson.tmm

jackson.tmm

努力學習,成為更好的自己

14 日誌
28 分類
50 標籤
GitHub Facebook Instagram Linkedin
標籤雲
  • Array
  • String
  • Recursion
  • Binary tree
  • Dynamic programming
  • Learning
  • Recursive
  • Stack
  • Creational
  • Priority queue
© 2022 - 2023 Jackson.tmm
Powered by - Hugo v0.111.2 / Theme by - NexT
0%