new:新增Cangjie题解:最长公共子序列

This commit is contained in:
Chemxy
2024-09-13 23:05:37 +08:00
parent 60f34628eb
commit d742497b52

View File

@ -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]
}
```
<p align="center">