当前位置:网站首页>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];
}
}
边栏推荐
- Unity性能优化------渲染优化(GPU)之Occlusion culling(遮挡剔除)
- The reverse order pairs in the "sword finger offer" array
- Network equipment hard core technology insider router Chapter 9 Cisco asr9900 disassembly (II)
- Several basic uses of tl431-2.5v voltage reference chip
- 资本频频加码,急于上市的和府捞面有多“疯狂”?
- Some binary bit operations
- STM32 CAN 通信 滤波设置问题
- 网络设备硬核技术内幕 路由器篇 19 DPDK(四)
- The first common node of the two linked lists of "Jianzhi offer"
- Unity最简洁的对象池实现
猜你喜欢

Unity性能优化------渲染优化(GPU)之Occlusion culling(遮挡剔除)

适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制

仅做两项修改,苹果就让StyleGANv2获得了3D生成能力
USB接口电磁兼容(EMC)解决方案

Leetcode 190. reverse binary bit operation /easy

Unity performance optimization ----- LOD (level of detail) of rendering optimization (GPU)

3.3-5v conversion

基于stm32的数字示波器设计方案

LeetCode 190. 颠倒二进制位 位运算/easy

Google team launches new transformer to optimize panoramic segmentation scheme CVPR 2022
随机推荐
STM32 can communication filter setting problem
Leetcode 244周赛-赛后补题题解【西兰花选手】
LeetCode 74. 搜索二维矩阵 二分/medium
STM32F10x_ Hardware I2C read / write EEPROM (standard peripheral library version)
4种单片机驱动继电器方案
Unity性能优化------渲染优化(GPU)之LOD(Level of detail)
IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一
Network equipment hard core technology insider router Chapter 11 Cisco asr9900 disassembly (V)
魔塔项目中的问题解决
TCC
Leetcode 244 week competition - post competition supplementary question solution [broccoli players]
Network equipment hard core technology insider router Chapter 3 Jia Baoyu sleepwalking in Taixu Fantasy (middle)
Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry
generic paradigm
How to edit a framework resource file separately
网络设备硬核技术内幕 路由器篇 5 汤普金森漫游网络世界(上)
Some binary bit operations
Kubernetes CNI classification / operation mechanism
分布式锁
USB接口电磁兼容(EMC)解决方案