[Algorithm learning] maximum substring Problem

Source: Internet
Author: User

Http://vistb.net/2011/07/max-subarray-problem/ from

I encountered this problem today and want to share it with you again.

Reading 《AlgorithmIn the introduction, I talked about the largest substring. The book mentions three algorithms, with time complexity in sequence: O (N ^ 2), O (NLogN) and O (n ). It seems interesting. Write it out and share it.

Definition: Maximum sub-array problem)

The maximum substring problem, also known as the maximum continuous substring problem, is to give an integer array a with the length of N, and then require the continuous substrings with the maximum value. That is, Max (sum (A [I... J]) is obtained, where 1 ≤ I ≤ j ≤ n.

Solution 1: Brute Force)

The first method is also the most stupid one. Select the sum (A [I .. J]) of all possible combinations of I and J. Obviously, because the possible number of combinations is C (n, 2) = O (N ^ 2), the complexity of the algorithm is O (n ^ 2 ).

Solution 2: Divide and conquer)

Note that the maximum substring A [I. J] of a [1 .. n] may come from one of the following three situations:

    • 1 ≤ I ≤ j ≤ n/2, that is, the maximum substring appears in the left half of the original string
    • N/2 ≤ I ≤ j ≤ n, that is, the maximum substring appears in the right half of the original string
    • 1 ≤ I ≤ n/2 ≤ j ≤ n, that is, the largest substring is from the half of the original string, and the portion is from the right half of the original string.

Therefore, we can calculate the solutions under the three conditions respectively, and then select the best one of the three solutions as the solution of the entire problem.

In the first two cases, the problem is actually reduced by divide ). Change the maximum substring of an array whose length is n to the maximum substring of an array whose length is n/2. There are three possible solutions to the problems that have become smaller. For the first two cases, we still adopt the subdivision method. However, will such a subdivision have a header? Of course, at the end, we will get an array with a length of 1. In this case, we do not have to consider three situations, this is because the largest substring is the conquer of the substring with a length of 1 ).

In the third case, we adopt another strategy, that is, solving the problem directly instead of converting the problem into the same smaller-scale problem. First, obtain a [I... n/2] to make it a [1... the maximum substring of n/2]. Here, we can perform a traversal. the time complexity is O (n ). Similarly, we can find the maximum substring of a [n/2... J] to make it a [n/2... n. Combine the two largest substrings to obtain the largest substrings that span the middle of the string.

To sum up, if T (n) is used to represent the time consumed to solve the maximum substring problem of an array of N in this method, there are:

    • T (n) = 2 T (n/2) + O (N), n> 1
    • T (1) = O (1), n = 1

It is not difficult to find that the time complexity for solving the entire problem is O (nLogN ).

Algorithm pseudoCodeAs follows (from introduction to algorithms):

Solution 3: Dynamic Planning)

The time complexity is O (nLogN) It is already an ideal result. However, in the exercises that follow the book, another solution based on dynamic planning is proposed, which can reduce the time complexity to O (n ).

Specifically, if we already know a [1 .. j] The maximum substring of this array, then a [1... the maximum substring of (J + 1)] may come from two situations: first, it and a [1... the maximum substring of J] is the same, and the second is a [1... the maximum substring of (J + 1)] has a [I... (J + 1)], where 1 ≤ I ≤ j + 1. For the second case, if we already know the maximum substring in all substrings whose tails are J, We can get all the substrings whose tails are (J + 1) within the constant time).

Based on the above idea, we only need to start from the leftmost part of the string (at this time, the largest substring is the first element of the string), gradually expand to the right until the complete string is processed, to obtain the largest substring.

The pseudo code of the algorithm is as follows (self-written, not necessarily true ):

Extended: K maximum substring problem (K maximum sub-array problem)

Based on the general largest substring problem, we can expand the K largest substring problem, that is, how to find the first K largest substrings. This is another type of problem.

References:

    • Chapter 4 Section 1: <Introduction to algorithms (3ed)> Chapter 4 Section 1

No related posts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.