Leetcode-longest palindromic substring

Source: Internet
Author: User

Given a stringS, Find the longest palindromic substring inS. You may assume that the maximum lengthSIs 1000, and there exists one unique longest palindromic substring.

Query the longest response string

 

Personal thoughts:

1. brute-force cracking: Find all the substring O (n2) of the string, judge whether the substring is an O (n) of the input string, and record the longest substring, the total time complexity is O (N3), which will time out.

2. Using the manacher algorithm, this algorithm is very clever. the time complexity and space complexity are both O (N). Here is an article about this algorithm: Explain:

1. First, we need to understand that this algorithm is optimized based on the enumeration center point algorithm. The enumeration center point algorithm is to expand to the left and right sides with each character as the center point of the input string, to obtain the longest return string, but note that the algorithm must consider odd and even numbers, such as 1221 and 121"

2. Before using this algorithm for computation, we need to process the original string S. It can be illustrated with an example, for example, string S = "12212321 ", after processing, it becomes a string T = "^ #1 #2 #2 #1 #2 #2 #3 #2 #1 # $". Then, t is operated, there are two reasons for this: the first reason is to insert the '#' character between characters, which is used to uniformly consider the odd and even numbers. If the return string is an even number, it will take the '#' character as the center point. If it is an odd number, it will take the character in the original string as the center point. The second reason is to insert two different special characters in the header and tail, avoid handling out-of-boundary situations.

3. Set an array P to record the return radius at the center point of each character, that is, the length of the right or left expansion. This length does not include the center point. Therefore, the length is the number of non-# characters contained in the return string, that is, the number of valid characters. For example, to calculate P [5] (the starting character of t starts from 0 ), obviously, the input string centered on T [5] is "#1 #2 #2 #1 #". If it is expanded 4 characters to the right, P [5] = 4, it is also equal to the number of valid characters (,) of the input string.

4. Now, you have to go to the topic of this algorithm. If it is just an algorithm for enumerating the center point, P [I] increments from 0 each time, but here, we will use the nature of the ECHO to accelerate this process. When we want to calculate P [I], if T [I] falls into the return radius of T [J, it has a good nature. See:

Let's skip the differences between the three graphs. Let's look at the three subscripts I, j, and K. I falls into the return radius of J, where k is the symmetry point between I and j, in fact, the back-to-text string is symmetric based on the central character. to grasp this character, we first throw the conclusion that P [I]> = min (P [K], J + P [J]-I). How can we draw this conclusion? This will analyze the situation in step 3.

In 1st cases, K's return range is still in J's return range (excluding K-P [k] = J-P [J ), in this case, according to the symmetry of J-string and K-string, we can conclude that P [I]> = P [k] Can I-string be expanded out? If yes, we can know the range of K-return strings Based on the symmetry between I-and K-return-string, not just the length at this time, so P [I] = P [k]

In 2nd cases, K's return range exceeds J's return range, and the part beyond the guarantee range of J's return string, therefore, P [I]> = J + P [J]-I, because the range of J + P [J]-I is the maximum guaranteed range, so can I expand the string? If yes, the range of the J-return string can be known Based on the I-return string and K-return string, not just the length at this time, and the contradiction is obtained, so P [I] = J + P [J]-I

In 3rd cases, K's return range is exactly on J's Return Boundary. According to the partial inference of 1, P [I]> = P [K], however, due to the coincidence between K's return boundaries and J's return boundaries, I may expand out, because at this time the expansion is not restricted by J's return strings, in this case, we need to expand outward to increase the length of the I-return String Based on P [I] = P [K ].

Of course, all of the above is that I falls within the range of a center point J. If I is not within the range of any center point, it must start with P [I] = 0, continue to test expansion to calculate the length of the retrieval

5. Finally, we need to convert the length and substring calculated in the T string into the length and substring in the original string S. This is easier, so we won't talk about it anymore.

Code:

1 # include <string> 2 # include <vector> 3 4 using namespace STD; 5 6 class solution {7 public: 8 string longestpalindrome (string s) {9 If (S. empty () 10 {11 return s; 12} 13 14 string T = preprocess (s); 15 const int length = T. length (); 16 vector <int> P; // The return radius of each character at the center point, excluding the center itself. Therefore, the return radius is the actual number of characters contained in the return string 17 p. resize (length); 18 P [1] = 0; 19 int c = 1, E = 1; // overwrite the largest center subscript and boundary point subscript of the input string 20 int maxc = 1; // The center of the maximum return radius Heart point subscript 21 22 for (INT I = 2; I <length-2; ++ I) 23 {24 p [I] = I <e? Min (P [2 * c-I], e-I): 0; 25 26 while (T [I + P [I] + 1] = T [I-P [I]-1]) 27 {28 + + P [I]; 29} 30 31 if (I + P [I]> E) 32 {33 C = I; 34 E = I + P [I]; 35} 36 37 If (P [I]> P [maxc]) 38 {39 maxc = I; 40} 41} 42 43 return S. substr (maxc-P [maxc]-1)/2, P [maxc]); 44} 45 46 string preprocess (string S) 47 {48 string T = "^ #"; 49 int length = S. length (); 50 51 for (INT I = 0; I <length; ++ I) 52 {53 t. insert (T. end (), s [I]); 54 T. insert (T. end (), '#'); 55} 56 T. insert (T. end (), '$'); 57 58 return t; 59} 60 };
View code

 

Leetcode-longest palindromic substring

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.