# Search for the longest response string

Source: Internet
Author: User

Since the original text does not explain what the background is, let me explain the meaning of the Background: the so-called background string means that the string is the same as the reading and reverse reading results, for example, the string "aba" is the same as the reverse reading.

Searching for the longest substring in a string is a classic algorithm problem. In this article, I will summarize three different solutions.

1. The simplest method

Most directly, we can test whether each substring is a response. The time complexity of this operation is O (n ^ 3 ). This is just the beginning and we need a better solution. The following is the implementation code:

```bool IsPalindromic(string& str){int nLen = str.length();for (int i=0;i

nMaxPlinLen){strLongestPalindromic = strTemp;nMaxPlinLen = nNewLen;}}}return strLongestPalindromic;}
```

2. Dynamic Planning

Suppose S represents a string, and I and j represent the subscript of S string, respectively, define a two-dimensional array table [I] [j] to indicate whether the substring formed by the characters from subscript I to subscript j in string S is a return string, when the value is 1, the table is yes, and the value 0 indicates no.

The theorem is as follows:

`table[i][i] == 1;table[i][i+1] == 1  => s.charAt(i) == s.charAt(i+1) `
The following inferences can be drawn:

`table[i][j] == 1 => table[i+1][j-1] == 1 && s.charAt(i) == s.charAt(j)`

The time complexity is O (n ^ 2) and the space complexity is O (n ^ 2). The Code implementation is as follows:

```String FindLongestPalindrome2 (string & str) {if (str. empty () {return NULL;} int nLen = str. length (); if (nLen <= 1) {return str;} int nMaxLen = 0; string strLongestStr;/* int table [nLen] [nLen]; */int ** table = (int **) malloc (sizeof (int *) * nLen); for (int I = 0; I

NMaxLen) {strLongestStr = str. substr (I, k) ;}} else {table [I] [j] = 0 ;}}for (int I = 0; I

If the table is output, we can easily find the longest return string position in the table. The table can visually reflect the number of replies and the length of each response, this is better than the first method.

3. A simple implementation method
The time complexity of this method is O (n ^ 2) Space responsibility is O (1 ). As for how simple it is, read the following code:

String Helper (string & str, int nBegin, int nEnd) {while (nBegin> = 0 & nEnd

StrLongest. length () {strLongest = strTemp;} // because the background may contain an even number of letters, strTemp = Helper (str, I, I + 1); if (strTemp. length ()> strLongest. length () {strLongest = strTemp;} return strLongest ;}

This method basically means to search for the longest return string with a single character or two characters as the center.

4. Manacher Algorithm
The algorithm has O (n) in time and O (1) in space. However, since it is extremely complicated and not a typical practice, we will not take the time to introduce it.

Summary: The above mainly summarizes three methods for retrieval, which are further summarized as follows.
(1) completely traverse each sub-string, record its length, and check whether it is a back-to-text, easy to understand, but the time complexity is O (n ^ 3 ), the space complexity is O (1 ).
(2) Based on the dynamic programming principle, we first determine whether a single character is a return (which must be), and then determine whether the double character is a return, then, use inferences to find out whether the strings of various lengths are input objects. A table can clearly reflect the existence of input objects, which is not available in methods 1 and 3, however, the space complexity is increased to O (n ^ 2), but the time complexity is reduced to O (n ^ 2 ).
(3) The last step is to use the center-point diffusion method to find the longest retrieval. This method is simple and efficient. The time complexity is O (n ^ 2) and the space complexity is O (1 ).
However, both method (1) and method (3) have one disadvantage: if there are multiple longest reply substrings, only the first longest reply substring is obtained, however, method 2 can reflect all the longest response strings.
All the programs in this translation have been debugged by themselves, which is different from the original program. I don't know whether java syntax is different from c ++, I personally think that the second program in the original article has problems in subscript processing.

```
Related Keywords:

### 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.