From d742497b528e028cff4f756524ed1d9ddc24c8da Mon Sep 17 00:00:00 2001 From: Chemxy Date: Fri, 13 Sep 2024 23:05:37 +0800 Subject: [PATCH] =?UTF-8?q?new=EF=BC=9A=E6=96=B0=E5=A2=9ECangjie=E9=A2=98?= =?UTF-8?q?=E8=A7=A3=EF=BC=9A=E6=9C=80=E9=95=BF=E5=85=AC=E5=85=B1=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1143.最长公共子序列.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md index 7fa7bb68..93df987e 100644 --- a/problems/1143.最长公共子序列.md +++ b/problems/1143.最长公共子序列.md @@ -399,6 +399,24 @@ int longestCommonSubsequence(char* text1, char* text2) { } ``` +### Cangjie +```cangjie +func longestCommonSubsequence(text1: String, text2: String): Int64 { + let n = text1.size + let m = text2.size + let dp = Array(n + 1, {_ => Array(m + 1, repeat: 0)}) + for (i in 1..=n) { + for (j in 1..=m) { + if (text1[i - 1] == text2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1 + } else { + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + } + } + } + return dp[n][m] +} +```