当前位置:网站首页>9. < tag dynamic programming and subsequence, subarray> lt.718. Longest repeated subarray + lt.1143. Longest common subsequence
9. < tag dynamic programming and subsequence, subarray> lt.718. Longest repeated subarray + lt.1143. Longest common subsequence
2022-07-25 19:57:00 【Caicai's big data development path】
lt.718. Longest repeating subarray
[ Case needs ]

[ Thought analysis ]
Be careful : Usually in the title , The subarray defaults to
Continuous subsequences. And the subarray is more suitable for dp.
- Five steps of dynamic rule :
- determine dp The meaning of arrays and subscripts :
dp[i][j]: By subscript i - 1 Bit terminated array A, And the following j - 1 An array ending with B, The longest repeating subarray length is dp[i][j]
Particular attention : “ By subscript i - 1 For the end of A” Mark it must be With A[i-1] String ending with
- Determine the recurrence formula
according to dp[i][j] The definition of , dp[i][j] The state of can only be determined by dp[i - 1][j - 1] derived .
When A[i - 1] and B[j - 1] On equal terms , dp[i][j] = dp[i - 1][j - 1] + 1;
According to the recurrence formula, we can see , Traverse i and j From you to 1 Start !
- dp Initialization of an array ;
- Determine the traversal order
- Give an example to deduce dp Array
- Take for example 1 in ,A: [1,2,3,2,1],B: [3,2,1,4,7] For example , Draw a picture dp The state of the array changes , as follows :

[ Code implementation ]
class Solution {
public int findLength(int[] nums1, int[] nums2) {
// Subsequences are discontinuous by default , Subarray is continuous by default
//1. The continuity of the current state can be deduced from the continuous state of the preceding subsequence , So you can use a dynamic gauge ;
int len1 = nums1.length;
int len2 = nums2.length;
//1. dp Array , dp[i] Express i The longest repeated continuous subarray in the subarray of ;
// dp[i][j] Indicates subscript i - 1 Array of nums1 And the subscript is j - 1 Array of nums2, Their common continuous coincidence array is dp[i][j]
int[][] dp = new int[len1 + 1][len2 + 1];
//2. The recursive formula
// dp[i][j] = dp[i - 1][j - 1] + 1;
//3. Initialization of an array
// dp[0][0] = 0;
//4. Determine the traversal order , It doesn't matter the order , Is to find the coincidence data of two arrays , Everyone is the same outside ,
int res = 0;
for(int i = 1; i < len1 + 1; i++){
for(int j = 1; j < len2 + 1; j++){
if(nums1[i - 1] == nums2[j - 1]){
dp[i][j] = dp[i - 1][j - 1] + 1;
if(dp[i][j] > res) res = dp[i][j];
}
}
}
return res;
}
}
To be added , The practice of scrolling arrays ;
lt.1143. Longest common subsequence
[ Case needs ]

[ Thought analysis ]
Simply put, let's find subsequences , Generally, it is discontinuous , This question and the above 718. Longest repeating subarray The difference is that it is not required to be continuous , But in relative order , namely :“ace” yes “abcde” The subsequence , but “aec” No “abcde” The subsequence .
The five parts of dynamic rule are as follows :
- determine dp Array and its subscript meaning
dp[i][j]: The length is [0, i - 1] String text1 And length [0, j - 1] String text2 The longest common subsequence of is dp[i][j]
Some students will ask : Why define the length as [0, i - 1] String text1, Defined as a length of [0, i] String text1 Is it not fragrant ?
This definition is for the convenience of later code implementation , If you have to define the length as [0, i] String text1 It's fine too , You can try !
- Determine the recurrence formula
There are mainly two situations : text1[i - 1] And text2[j - 1] identical ,text1[i - 1] And text2[j - 1] inequality
a. If text1[i - 1] And text2[j - 1]identical, So we found a common element , therefore dp[i][j] = dp[i - 1][j - 1] + 1;
b. If text1[i - 1] And text2[j - 1]inequality, Then look at it.text1[0, i - 2] And text2[0, j - 1] The longest common subsequence ofandtext1[0, i - 1] And text2[0, j - 2] The longest common subsequence of, Take one of the biggest . namely :dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if(text1[i - 1] == text[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]);
}
- dp How to initialize an array
Have a look first dp[i][0] How much should it be ?test1[0, i-1] And the longest common subsequence of empty string is naturally 0, therefore dp[i][0] = 0;
Empathy dp[0][j] It's also 0. Other subscripts are gradually covered with the recursive formula , The initial value can be as much as , Then unify the initial 0.
- Determine the traversal order
- Give an example to deduce dp Array
[ Code implementation ]
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
//1. dp Array
// dp[i][j] Express text1 and text2 Two strings in 0 ~ i- 1 and 0~j-1 The longest common subsequence in the range ;
int len1 = text1.length();
int len2 = text2.length();
int[][] dp = new int[len1 + 1][len2 + 1];
//2. Determine the recurrence formula
/** dp[i][j] = dp[i - 1][j - 1] + 1; dp[i][j] = dp[i - 1][j], dp[i][j - 1]; */
//3. initialization , All for 0
//4. Determine the traversal order , From left to right , From top to bottom
for(int i = 1; i < len1 + 1; i++){
for(int j = 1; j < len2 + 1; 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[len1][len2];
}
}
边栏推荐
- Add a subtitle of 3D effect to the container
- RepVGG网络中重参化网络结构解读【附代码】
- [wp]ctfshow-web introductory information collection
- Mutual conversion of camera internal parameter matrix K and FOV
- Introduction to web security ICMP testing and defense
- Legal mix of collations for operation 'Union' (bug record)
- Software designer afternoon real topic: 2009-2022
- Is there a "fingerprint" in the structure of AAAI 2022 | Gan? Generating network structure from forged image traceability
- 统信UOS下配置安装cocos2dx开发环境
- C # add multi line and multi column text watermark in word
猜你喜欢

Ml programming skills:
![[artifact] screenshot + mapping tool snipaste](/img/d2/a9a706a114641094e32ab5c6193f58.png)
[artifact] screenshot + mapping tool snipaste

Security foundation 6 - vulnerability recurrence

Research and application of servo driver in robot

PMP practice once a day | don't get lost in the exam -7.25

YOLOv7论文部分解读【含自己的理解】

蓝桥杯基础练习——矩阵的回形取数(C语言)
Detailed evaluation of current popular redis visual management tools

Recommendations on how to install plug-ins and baby plug-ins in idea

Day7:有序二叉树(二叉搜索树)
随机推荐
国内常见php的CMS建站系统情况分析
YOLOv7论文部分解读【含自己的理解】
Nezha d1-h test microbench
TFIDF examples and explanations
Detailed evaluation of current popular redis visual management tools
连接数据库警告 Establishing SSL connection without server‘s identity verification is not recommended.
Global configuration and page configuration of wechat applet development
由一个蓝桥杯基础题报时助手而引出的常见误区
NPM semantic version control, solution console prop being mutated: "placement" error
软件设计师下午真题:2009-2022
Creative drop-down multi choice JS plug-in download
手机端触摸图片slider轮播插件photoswipe.js
蓝桥杯基础练习——矩阵的回形取数(C语言)
Oracle database download, installation, use tutorial and problem summary
VMware virtual machine download, installation and use tutorial
[good book recommendation] - authoritative guide to Ethernet (2nd Edition)
Digital informatization (enumerate assumptions first, and then see whether the conditions are met) (1089 werewolf kill - simple version)
VMware 虚拟机下载、安装和使用教程
高数_第3章重积分 学习体会与总结
High number_ Chapter 3 learning experience and summary of multiple integral




