当前位置:网站首页>2022.05.26(LC_1143_最长公共子序列)
2022.05.26(LC_1143_最长公共子序列)
2022-06-10 18:16:00 【Leeli9316】

方法:动态规划
①确定状态:dp[i][j]表示长度[0, i - 1]的字符串text1与长度[0, j - 1]的字符串text2的最长公共子序列;
②转移方程:dp[i][j]=dp[i−1][j−1]+1, 当 text1[i - 1] == text2[j - 1];
dp[i][j]=Math.max(dp[i−1][j],dp[i][j−1]), 当 text1[i - 1] != text2[j - 1];
③初始条件和边界情况:dp[i][0] = 0;dp[0][j] = 0;
④计算顺序:因为 dp[i][j] 由 dp[i−1][j−1],dp[i - 1][j] 和 dp[i][j - 1]推出,所以从小到大遍历。
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int m = text1.length(), n = text2.length();
//dp[i][j]表示长度[0, i - 1]的字符串text1与长度[0, j - 1]的字符串text2的最长公共子序列
int[][] dp = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m][n];
}
}注意:如果当前两个字符不等,t1[i]!=t2[j]的话,那么长度最少也是dp[i-1][j-1],但这还不够,因为我们希望拿到之前的比较中尽可能大的长度。那么当前字符已经不相等的情况下,就应该把当前的字符也放入到之前的比较中,那么一定有dp[i][j-1]和dp[i-1][j]>=dp[i][j]。简单来说,dp[i][j]的值应该从dp[i-1][j-1],dp[i][j-1],dp[i-1][j]三者中取,但dp[i-1][j-1]≤dp[i][j]≤另外两个,故比较另外两个,取较大的。
边栏推荐
- Some summary about YUV format
- Adobe Premiere foundation - opacity (matte) (11)
- RK1126 新添加一个模块
- How to transform digital transformation? Which way?
- VS从txt文件读取中文汉字产生乱码的解决办法(超简单)
- In the digital age, why should enterprises make digital transformation?
- Analysis of optical storage direct flexible power distribution system
- Introduction to ad18 device library import
- Analysis of Muduo source code -- an analysis of the rigor, efficiency and flexibility of Muduo library code design with three slices
- 数据治理经典6大痛点?这本书教你解决
猜你喜欢

How to set up salesmartly for Google Analytics tracking

Openssl1.1.1 compilation error can't locate win32/console pm in @INC

5. golang generics and reflection

AEC: analysis of echo generation causes and echo cancellation principle

如何设置 SaleSmartly 以进行 Google Analytics(分析)跟踪

【 random talk 】 congratulations on getting the title of CSDN expert. Your efforts will eventually pay off

数据库防火墙闪亮登场(好文共赏)

Adobe Premiere基础-不透明度(蒙版)(十一)

Use of uiautomator2 automated test tool

In the digital age, why should enterprises make digital transformation?
随机推荐
Chapter 1 SQL operators
RK1126 新添加一个模块
单纯形法代码求解(含超详细代码注释和整个流程图)
Upgrade the playing method of snatching singing, integrate the climax clips of genuine music and real-time scoring ability~
Wireshark learning notes (II) detailed explanation of forensics analysis cases
Adobe Premiere基础-不透明度(蒙版)(十一)
基于SSM流量计量云系统的设计与实现.rar(论文+项目源码)
Data URL
阵列信号处理仿真之四——Z变换分析阵列多项式
Request header field XXXX is not allowed by access control allow headers in preflight response
How to transform digital transformation? Which way?
[Agency] 10 minutes to master the essential difference between forward agency and reverse agency
Ruixin micro rk1126 platform platform porting libevent cross compiling libevent
Analysis of Muduo source code -- an analysis of the rigor, efficiency and flexibility of Muduo library code design with three slices
3. getting started with golang concurrency
Google Earth engine (GEE) -- Copernicus atmosphere monitoring (CAMs) global aerosol AOI near real-time observation data set
【数据库语言SPL】写着简单跑得又快的数据库语言 SPL
Jsp基于ssm项目实验室管理系统设计与现实.doc
Seata installing the window environment
AgI foundation, uncertain reasoning, subjective logic Ppt1
https://leetcode.cn/problems/longest-common-subsequence/solution/fu-xue-ming-zhu-er-wei-dong-tai-gui-hua-r5ez6/