Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of the longest common substring of C++

Shulou Source: shulou.com Published: 2022-05-31 14:31:21 09月11日 Update

This article mainly explains "how to solve the problem of the longest common substring of C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn how to solve the problem of the longest common substring of C++.

1 description

There are two strings (which may contain spaces). Find the longest common contiguous substring and output its length. (length less than 1000)

For example:

Input: abcde bcd

Output: 3

2 parsing

1. Two strings are formed into a two-dimensional matrix with rows and columns respectively.

2. compare whether each point in the two-dimensional matrix is equal in the row and column characters, and the equal value is set to 1, otherwise it is set to 0.

3. The longest common substring can be found by finding the longest diagonal with a value of 1.

For example: str=acbcbcef,str2=abcbced, the longest common substring of str and str2 is bcbce, and the longest common substring length is 5.

The two-dimensional matrix we can get for the above two strings is as follows:

As you can see from the figure above, str1 and str2 have 5 common substrings, but the longest common substring is 5.

In order to further optimize the efficiency of the algorithm, we can calculate the length of the longest common substring when calculating the value of a two-dimensional matrix, that is, the value of a two-dimensional matrix element evolves from record [I] [j] = 1 to record [I] [j] = 1 + record [iMuk 1] [JMAT 1], thus avoiding the subsequent operation of finding the diagonal length. The modified two-dimensional matrix is as follows:

The recursive formula is:

When A [I]! = B [j], dp [I] [j] = 0

When A [I] = = B [j]

If I = 0 | | j = = 0pm DP [I] [j] = 1

Otherwise dp [I] [j] = dp [I-1] [j-1] + 1

3 Code violence method public int getLCS (String s, String S2)

{

If (s = = null | | t = = null)

{

Return 0

}

Int L1 = s.length ()

Int L2 = t.length ()

Int res = 0

For (int I = 0; I < 11; iTunes +)

{

For (int j = 0; j < 12; jacks +)

{

Int m = I

Int k = j

Int len = 0

While (m < 11 & & k < 12 & & s.charAt (m) = = t.charAt (k)) {

Len++

MFG +

Kicking +

}

Res = Math.max (res, len)

}

}

Return res

}

Dynamic programming public int getLCS (String s, String t)

{

If (s = = null | | t = = null)

{

Return 0

}

Int result = 0

Int sLength = s.length ()

Int tLength = t.length ()

Int [] [] dp = new int [sLength] [tLength]

For (int I = 0; I < sLength; iTunes +) {

For (int k = 0; k < tLength; knot +) {

If (s.charAt (I) = = t.charAt (k)) {

If (I = = 0 | | k = = 0) {

Dp [I] [k] = 1

} else {

Dp [I] [k] = dp [I-1] [k-1] + 1

}

Result = Math.max (dp [I] [k], result)

} else {

Dp [I] [k] = 0

}

}

}

Return result

}

Simplify the recursive formula:

When A [I]! = B [j], dp [I] [j] = 0

Otherwise dp [I] [j] = dp [I-1] [j-1] + 1

It all boils down to one formula, and the default value of the two-dimensional group is 0.

Public int getLCS (String s, String t)

{

If (s = = null | | t = = null)

{

Return 0

}

Int result = 0

Int sLength = s.length ()

Int tLength = t.length ()

Int [] [] dp = new int [sLength + 1] [tLength + 1]

For (int I = 1; I

Tags: Longest two-dimensional matrix length problem cantilever character two formula string learning content diagonal diagonal output above code element dynamic that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MariaDB Xiaomi Shulou Tech Info vpn