[Conversion] Longest reply substring-Four Solutions

Source: Internet
Author: User

Question:

Recently I have just studied dynamic planning. I feel that it is indeed an obscure algorithm. solution 2 is to use dynamic planning. Generally, it is to use arrays to record the tried solutions, pruning is provided for subsequent solutions. For this question, solution 1 and solution 3 are easy to understand.

Solution 1: Use two for loops to find all the substrings. The third for loop is used to determine whether the substrings are in the background. If the substrings are in the background, they are replaced if they are longer than the retrieved substrings, algorithm time efficiency is O (n ^ 3)

Solution 3: Use the for loop to traverse every character in the string. Every character found is centered on this and expanded to both sides to check whether the left and right strings are equal. However, there are two types of input, one is an odd number and the other is an even number, as follows:

Odd text return: ABA

Even retrieval: ABBA

Therefore, there are two cases. Algorithm time efficiency is O (n ^ 2)

 

Original article:

I have registered hihocoder before. Now I see the special topic on programming strings. I want to write it myself.

The reply refers to reading and reading backwards. The result is similar, such as abcba or Abba.

The topic is the longest response substring in a string.

1. Violence Law

The most likely thing to think of is brute-force cracking. Find every substring, then judge whether it is a return, and find the longest one.

Calculate the time complexity O (N ^ 2) of each substring and determine whether the substring is input O (N). Therefore, the time complexity is O (n ^ 3 ).

 

String findlongestpalindrome (string & S) {int length = S. size (); // String Length int maxlength = 0; // the maximum length of the retrieval string int start; // The start address of the longest retrieval string for (INT I = 0; I <length; I ++) // start address for (Int J = I + 1; j <length; j ++) // end address {int tmp1, tmp2; for (tmp1 = I, tmp2 = J; tmp1 <tmp2; tmp1 ++, tmp2 --) // you can call this operation to determine whether or not to return a message {If (S. at (tmp1 )! = S. at (tmp2) break;} If (tmp1> = tmp2 & J-I> maxlength) {maxlength = J-I + 1; Start = I ;}} if (maxlength> 0) return S. substr (START, maxlength); // return NULL for the substring ;}

 

2. Dynamic Planning

 

The substring of a return string is also a return string. For example, if p [I, j] (a substring that ends with J starting with I) is a return string, then P [I + 1, j-1] is also a return string. In this way, the longest response string can be broken down into a series of sub-problems. In this case, extra space O (N ^ 2) is required, and the algorithm complexity is O (n ^ 2 ).

First, define the state equation and the transfer equation:

P [I, j] = 0 indicates that the substring [I, j] is not a return string. P [I, j] = 1 indicates that the substring [I, j] is a return string.

P [I, I] = 1

P [I, j] {= P [I + 1, J-1], if (s [I] = s [J])

= 0, if (s [I]! = S [J])

 

 

String findlongestpalindrome (string & S) {const int length = S. size (); int maxlength = 0; int start; bool P [50] [50] = {false}; For (INT I = 0; I <length; I ++) // initialization preparation {P [I] [I] = true; if (I <length-1 & S. at (I) = S. at (I + 1) {P [I] [I + 1] = true; Start = I; maxlength = 2 ;}} for (INT Len = 3; len <length; Len ++) // substring length for (INT I = 0; I <= length-len; I ++) // substring start address {Int J = I + len-1; // substring end address if (P [I + 1] [J-1] & S. at (I) = S. at (j) {P [I] [J] = true; maxlength = Len; Start = I ;}} if (maxlength >=2) return S. substr (START, maxlength); return NULL ;}

 

3. The center extension refers to the expansion of each letter of a given string to both sides as the center, so as to find the longest sub-return string. The algorithm complexity is O (n ^ 2 ). But there are two cases to consider: 1. For example, ABA, the length is an odd number. 2. To use Abba, the length is an even number.
String findlongestpalindrome (string & S) {const int length = S. size (); int maxlength = 0; int start; For (INT I = 0; I <length; I ++) // The length is odd {Int J = I-1, k = I + 1; while (j> = 0 & K <length & S. at (j) = S. at (k) {If (k-J + 1> maxlength) {maxlength = k-J + 1; Start = J;} j --; k ++ ;}} for (INT I = 0; I <length; I ++) // The length is an even number {Int J = I, K = I + 1; while (j> = 0 & K <length & S. at (j) = S. at (k) {If (k-J + 1> maxlength) {maxlength = k-J + 1; Start = J;} j --; k ++ ;}} if (maxlength> 0) return S. substr (START, maxlength); return NULL ;}

 

4. manacher can only solve the problem of returning strings with an odd length such as Aba, but cannot solve the problem of Abba. Therefore, special characters are added to the string. I added "#" to change Abba to a # B #. This algorithm is calculated based on the symmetry of the existing input string. The specific algorithm complexity is O (n). I didn't see it because there are two nested for loops. For details, refer to here. I did not filter out "#" in the test code.
# Define min (x, y) (x) <(y )? (X) :( y) # define max (x, y) (x) <(y )? (Y) :( X) string findlongestpalindrome3 (string s) {int length = S. size (); For (INT I = 0, k = 1; I <length-1; I ++) // Add # {s to the string. insert (k, "#"); k = K + 2;} length = length * 2-1; // Add # the string length int * rad = new int [length] (); rad [0] = 0; For (INT I = 1, j = 1, K; I <length; I = I + k) {While (I-j> = 0 & I + j <length & S. at (I-j) = S. at (I + J) J ++; rad [I] = J-1; For (k = 1; k <= rad [I] & rad [I-K]! = Rad [I]-K; k ++) // image, when rad [I-K] = rad [I]-K is stopped, in this case, do not compare rad [I + k] = min (RAD [I-K], Rad [I]-k) from j = 1; j = max (J-K, 0); // update J} int max = 0; int center; For (INT I = 0; I <length; I ++) {If (RAD [I]> MAX) {max = rad [I]; center = I ;}} return S. substr (center-Max, 2 * MAX + 1 );}

 

 

[Conversion] Longest reply substring-Four Solutions

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.