In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to find the longest public substring in leetcode". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to find the longest public substring in leetcode".
Longest common substring and longest common subsequence
A Substring is a continuous part of a string, and a Subsequence is a new sequence that never changes the order of the sequence and removes any elements from the sequence; more simply, the character position of the former (substring) must be continuous, while the latter (subsequence LCS) does not. For example, the longest common substring of the string acdfg and akdfc is df, while their longest common subsequence is adf.
Longest common substring
Suppose we know the longest common substring length of S1 [0: iMub 1], S2 [0: jMur1] from right to left, then the two strings are moved one bit to the right at the same time. If S1 [I] = = S2 [j], then S1 [0 I], s2 [0Raj] is the longest common substring length at iForce j position, and S2 [0: jmur1] is the longest common substring length from right to left + 1, otherwise it is 0. Record this length with a [I] [j], and the state transition equation is as follows:
If S1 [I] = = S2 [j] {
A [I] [j] = a [I-1] [jmur1] + 1
} else {
A [I] [j] = 0
}
IPay1 is used, so both are incremented, initialization is 0, if the initials are the same a [0] [0 j] = 1recoery a [0puri] [0] = 1
Func Lcs (S1 string, S2 string) string {if len (S1) = = 0 | len (S2) = 0 {return ""} a: = make ([] [] int, len (S1)) for I: = 0; I
< len(s1); i++ { a[i] = make([]int, len(s2)) if []byte(s1)[i] == []byte(s2)[0] { a[i][0] = 1 } } for j := 1; j < len(s2); j++ { if []byte(s1)[0] == []byte(s2)[j] { a[0][j] = 1 } } max := 0 ii := 0 jj := 0 for i := 1; i < len(s1); i++ { for j := 1; j < len(s2); j++ { if []byte(s1)[i] == []byte(s2)[j] { a[i][j] = a[i-1][j-1] + 1 } if a[i][j] >Max {max = a [I] [j] ii = I jj = j}} return string ([] byte (S1) [ii+1-a [ii] [jj]: ii+1])}
Longest common subsequence
Suppose that the longest common subsequence of S1 [0: iMur1], S2 [0: jmur1] is known, if S1 [I] = = S2 [j], then the length of S1 [0: iRaj] is S1 [0: iMur1], the longest common subsequence of S2 [0: jmur1] is + 1, otherwise, the larger of S1 [0: jmuri], S2 [0: jmur1] and S1 [0: iRAJ]. Record the length of the longest common subsequence with a [I] [j], and the state transition equation is:
If S1 [I] = = S2 [j] {
A [I] [j] = a [I-1] [jmur1] + 1
} else {
A [I] [j] = max (a [] I) [jmur1], a [I-1] [j])
}
IPay1 is used, so both are incremented, initialization is 0, if the initials are the same a [0] [0 j] = 1recoery a [0puri] [0] = 1
Func Lcs (S1 string, S2 string) int {if len (S1) = = 0 | len (S2) = 0 {return 0} a: = make ([] [] int, len (S1)) for I: = 0; I
< len(s1); i++ { a[i] = make([]int, len(s2)) if []byte(s1)[i] == []byte(s2)[0] { a[i][0] = 1 } } for j := 1; j < len(s2); j++ { if []byte(s1)[0] == []byte(s2)[j] { a[0][j] = 1 } } max := 0 for i := 1; i < len(s1); i++ { for j := 1; j < len(s2); j++ { if []byte(s1)[i] == []byte(s2)[j] { a[i][j] = a[i-1][j-1] + 1 } else if a[i][j-1] >A [I-1] [j] {a [I] [j] = a [I] [j]} else {a [I] [j] = a [I-1] [j]} if a [I] [j] > max {max = a [I] [j]} return max} all the contents of the article "how to find the longest common substring in leetcode" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.