当前位置:网站首页>Leetcode 1143. dynamic programming of the longest common subsequence /medium
Leetcode 1143. dynamic programming of the longest common subsequence /medium
2022-07-27 15:28:00 【Abcheche】
List of articles
1.Description
Given two strings text1 and text2, Returns the longest of these two strings Common subsequence The length of . If it doesn't exist Common subsequence , return 0 .
A string of Subsequence This is a new string : It is to delete some characters from the original string without changing the relative order of the characters ( You can also delete no characters ) After the composition of the new string .
for example ,“ace” yes “abcde” The subsequence , but “aec” No “abcde” The subsequence .
Two strings of Common subsequence Is the subsequence shared by these two strings .
2.Example
Example 1:
Input :text1 = "abcde", text2 = "ace"
Output :3
explain : The longest common subsequence is "ace" , Its length is 3 .
Example 2:
Input :text1 = "abc", text2 = "abc"
Output :3
explain : The longest common subsequence is "abc" , Its length is 3 .
Example 3:
Input :text1 = "abc", text2 = "def"
Output :0
explain : The two strings have no common subsequence , return 0 .
3.Solution
Reference resources : Add link description
First , Distinguish between two concepts : Subsequences can be discontinuous ; Subarray ( Substring ) The need is continuous ;
in addition , Dynamic programming also has a routine : When a single array or string needs dynamic programming , Dynamic programming can be dp[i] Defined as nums[0:i] I want to ask for the results ; When two arrays or strings need dynamic programming , Dynamic programming can be defined as two-dimensional dp[i][j] , Its meaning is in A[0:i] And B[0:j] Match to get the desired result .
1. State definition
For example, for this topic , Can define dp[i][j] Express text1[0:i-1] and text2[0:j-1] The longest common subsequence of . ( notes :text1[0:i-1] It means text1 Of The first 0 Elements to i - 1 Elements , Both ends contain )
The reason dp[i][j] The definition of is not text1[0:i] and text2[0:j] , It's for the convenience of being i = 0 perhaps j = 0 When ,dp[i][j] Represents a match between an empty string and another string , such dp[i][j] It can be initialized to 0.
2. State transition equation
After knowing the state definition , We begin to write the state transition equation .
When text1[i - 1] == text2[j - 1] when , Indicates that the last bit of the two substrings is equal , So the longest common subsequence is added 1, therefore dp[i][j] = dp[i - 1][j - 1] + 1; for instance , For example, for ac and bc for , The length of their longest common subsequence is equal to a and c The longest common subsequence length of 0 + 1 = 1.
When text1[i - 1] != text2[j - 1] when , Indicates that the last bit of two substrings is not equal , Then the state at this time dp[i][j] Should be dp[i - 1][j] and dp[i][j - 1] The maximum of . for instance , For example, for ace and bc for , The length of their longest common subsequence is equal to ① ace and b The longest common subsequence length of 0 And ② ac and bc The longest common subsequence length of 1 The maximum of , namely 1.
To sum up, the state transition equation is :
dp[i][j] = dp[i - 1][j - 1] + 1, When text1[i - 1] == text2[j - 1];
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]), When text1[i - 1] != text2[j - 1]
3. Initialization of state
Initialization is to see when i = 0 And j = 0 when , dp[i][j] What should be the value .
When i = 0 when ,dp[0][j] It means text1 Empty string in Follow text2 The longest common subsequence of , The result must be 0.
When j = 0 when ,dp[i][0] It means text2 Empty string in Follow text1 The longest common subsequence of , The result must be 0.
Sum up , When i = 0 perhaps j = 0 when ,dp[i][j] Initialize to 0.
4. Traversal direction and range
because dp[i][j] Depend on and depend on dp[i - 1][j - 1] , dp[i - 1][j], dp[i][j - 1], therefore ii and jj The traversal order of must be from small to large .
in addition , Because when ii and jj The value is 0 When ,dp[i][j] = 0, and dp The array itself is initialized to 0, therefore , Directly to ii and jj from 1 To traverse the . The end of the traversal should be the length of the string len(text1)len(text1) and len(text2)len(text2).
5. The final result
because dp[i][j] The meaning is text1[0:i-1] and text2[0:j-1] The longest common subsequence of . What we ultimately want is text1 and text2 The longest common subsequence of . So the result to be returned is i = len(text1) also j = len(text2) At the time of the dp[len(text1)][len(text2)].
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int M = text1.length();
int N = text2.length();
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];
}
}
边栏推荐
- 网络设备硬核技术内幕 路由器篇 9 CISCO ASR9900拆解 (二)
- 网络设备硬核技术内幕 路由器篇 CISCO ASR9900拆解 (一)
- 网络设备硬核技术内幕 路由器篇 5 汤普金森漫游网络世界(上)
- STM32 can -- can ID filter analysis
- IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items
- Kubernetes CNI classification / operation mechanism
- LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard
- 适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制
- Two stage submission and three stage submission
- Tools - common methods of markdown editor
猜你喜欢
随机推荐
网络设备硬核技术内幕 路由器篇 11 CISCO ASR9900拆解 (五)
The reverse order pairs in the "sword finger offer" array
LeetCode 781. 森林中的兔子 哈希表/数学问题 medium
DevEco Studio2.1运行项目报错
Internship: compilation of other configuration classes
网络设备硬核技术内幕 路由器篇 (10) CISCO ASR9900拆解 (四)
USB2.0接口的EMC设计方案
适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制
Inside router of network equipment hard core technology (10) disassembly of Cisco asr9900 (4)
华为鸿蒙模拟器去除顶部导航栏方法
STM32 can communication filter setting problem
Leetcode 244周赛-赛后补题题解【西兰花选手】
网络设备硬核技术内幕 路由器篇 CISCO ASR9900拆解 (一)
Wechat applet realizes music search page
Distributed lock
Unity mouse controls the first person camera perspective
STM32F10x_ Hardware I2C read / write EEPROM (standard peripheral library version)
分布式锁
STM32 CAN 通信 滤波设置问题
《终身成长》读书笔记(一)








