当前位置:网站首页>DP subsequence series problem
DP subsequence series problem
2022-07-22 17:32:00 【Curry won't throw three points】
package LeetCode.DP; public class OfferII095 { public int longestCommonSubsequence(String text1, String text2) { int n=text1.length(); int m=text2.length(); return helper(text1,0,text2,0); } private int helper(String text1, int n, String text2, int m) { if(n==text1.length()||m==text2.length()){ return 0; } if(text1.charAt(n)==text2.charAt(m)){ // The current two characters are the same return 1+helper(text1,n+1,text2,m+1); }else { return Math.max(Math.max(helper(text1,n+1,text2,m+1),helper(text1,n+1,text2,m)), helper(text1,n,text2,m+1)); } } }
- There is an optimization point , It's when we compare larger lengths , There is no need to compare helper(text1,n+1,text2,m+1), because n+1,m+1 The range of both strings of is less than n+1,m and n,m+1 String length of , So it must be shorter than the common subsequence of these two
- Why not recurse from front to back , Turn semantics into pre n Characters and before m The length of the longest common sequence of characters , Because when we compare a character , Our recursion is a recursive function that depends on a smaller range , That's not right , If we encounter a different character , We should compare the current character with the following character , See if you can find the same characters , If you define semantics like this , We just compare with the previous characters , That's not right , Because the recursive semantics is that the previous strings are better , It's meaningless to compare again
Pruning optimization
class Solution { public int longestCommonSubsequence(String text1, String text2) { int n=text1.length(); int m=text2.length(); int dp[][]=new int[n+1][m+1]; for (int []rows:dp) { Arrays.fill(rows,-1); } return helper(text1,0,text2,0,dp); } private int helper(String text1, int n, String text2, int m, int[][] dp) { if(n==text1.length()||m==text2.length()){ return 0; } if (dp[n][m]!=-1){ return dp[n][m]; } if(text1.charAt(n)==text2.charAt(m)){ // The current two characters are the same dp[n][m]= 1+helper(text1,n+1,text2,m+1, dp); return dp[n][m]; }else { dp[n][m] =Math.max(helper(text1,n+1,text2,m, dp), helper(text1,n,text2,m+1, dp)); return dp[n][m]; } } }
Bottom up dynamic planning
class Solution { public int longestCommonSubsequence(String text1, String text2) { int n=text1.length(); int m=text2.length(); int dp[][]=new int[n+1][m+1]; for (int i =1; i <=n; i++) { for (int j =1; j <=m; 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[n][m]; } }
- Use functions f(i, j) Representation string s1 Subscript from 0 Start to i Substring of s1[0...i] And string s2 Subscript from 0 Start to j Substring of s2[0...j] The length of the longest common subsequence of . about f(i, j), If s1[i] == s2[j], So it's equivalent to s1[0...i - 1] and s2[0...j - 1] Add a common character after the longest common subsequence of , That is to say f(i, j) = f(i - 1, j - 1) + 1. If s1[i] != s2[j], Then these two characters cannot appear in s1[0...i] and s2[0...j] In the common subsequence of . here s1[0...i] and s2[0...j] The longest common subsequence of is s1[0...i - 1] and s2[0...j] The longest common subsequence and s1[0...i] and s2[0...j - 1] The larger value in the longest common subsequence of , namely f(i, j) = max(f(i - 1, j), f(i, j - 1)). So the transition equation of state is
边栏推荐
- Openeuler is ambitious, open source huizhichuang future | 2022 open atom global open source summit openeuler sub forum is about to open
- 中国企业管理软件走向全球化国际化的路径探讨
- Spark advanced features, 220720,
- How can Tiktok cancel his account? What are the steps of the operation process?
- [leetcode] 814. Binary tree pruning
- 【C】 Branch and loop statements
- Network Security Learning (Qianfeng network security notes) 5 -- NTFS security permissions
- How much do you know about the basic features of SolidWorks| Four methods of extruding features
- 广度优先遍历(Breath First Search)
- 有人知道oracle cdc这个问题吗?source没有空值,但是查询定义的cdc表时说有空值,让修
猜你喜欢
132. Split palindrome string II
[pictures and texts] detailed tutorial of online one click reinstallation of win7 system
报表设计工具FastReport Online Designer V2022.1新改变全介绍
DP子序列系列问题
深度解决npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
SSRF vulnerability attack intranet redis recurrence
How to reinstall win11 system online with one click?
On the dilemma faced by non transferable reputation points NFT SBTS
牛客网刷题1【最小步数Fibonacci数列】
以CRM系统为案例讲解数据分析(重要性介绍及分析方法)
随机推荐
深度解决npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
浏览器页面的渲染流程
132. 分割回文串 II
【LeetCode】814. 二叉树剪枝
Classification of attention mechanism
信息安全CISP认证-大家关心哪些问题?
An annotation implementation method writes the returned data to the cache (facet, redis Implementation)
Network Security Learning (Qianfeng network security notes) 5 -- NTFS security permissions
NFS Shared Storage Service
ACL and net
Sparse array (sparse)
ABAQUS realizes modal calculation of two degree of freedom vibration system
牛客网刷题1【最小步数Fibonacci数列】
NFS shared storage service
[pictures and texts] detailed tutorial of online one click reinstallation of win7 system
基于 Flink CDC 实现海量数据的实时同步和转换
MySQL constraint_ Unique constraint
What if win11 encounters a problem and needs to restart?
Playbook 介绍
Solidworks基础特征你了解多少?| 拉伸特征的4种方法