Enter a string that outputs the maximum length of the symmetric substring in the string. For example, the input string "Google", because the longest symmetric substring in the string is "goog", so output 4.

Source: Internet
Author: User

Title: Enter a string that outputs the maximum length of the symmetric substring in the string. For example, the input string "Google", because the longest symmetric substring in the string is "goog", so output 4.

Analysis: Many people may have written to determine whether a string is a symmetric function, and the topic can be viewed as a reinforced version of the function. Introduction: To determine whether the string is symmetric

It is not a difficult thing to judge whether a string is symmetrical or not. We can get the string first and end two characters, and judge whether it is equal. If not equal, the string is certainly not symmetric. Otherwise, we will then judge whether the two characters inside are equal, and so on. Based on this idea, it is not difficult to write the following code:

////////////////////////////////////////////////////////////////

Whether a string between Pbegin and Pend is symmetrical?

////////////////////////////////////////////////////////////////

BOOL Issymmetrical (char* pbegin, char* pend)

{

if (Pbegin = null | | pend = NULL | | | pbegin > Pend)

return false;

while (Pbegin < pend)

{

if (*pbegin!= *pend)

return false;

pbegin++;

Pend--;

}

return true;

}

To determine if a string pstring is symmetric, we just need to call issymmetrical (Pstring, &pstring[strlen (pstring) –1). Solution One: O (n3) algorithm

Now we're trying to get the maximum length of the symmetric substring. The most intuitive approach is to get all the substring of the input string, and to judge whether it is symmetric individually. If a substring is symmetric, we get the length of it. By comparing this, you get the length of the longest symmetric substring. So we can write the following code:

////////////////////////////////////////////////////////////////

Get the longest length of it all symmetrical substrings

Time needed is O (t^3)

////////////////////////////////////////////////////////////////

int getlongestsymmetricallength_1 (char* pstring)

{

if (pstring = NULL)

return 0;

int symmeticallength = 1;

char* Pfirst = pstring;

int length = strlen (pstring);

while (Pfirst < &pstring[length-1])

{

char* pLast = Pfirst + 1;

while (PLast <= &pstring[length-1])

{

if (Issymmetrical (Pfirst, PLast))

{

int newlength = Plast-pfirst + 1;

if (Newlength > Symmeticallength)

Symmeticallength = Newlength;

}

plast++;

}

pfirst++;

}

return symmeticallength;

}

Let's analyze the time efficiency of the above method. Because we need a double while loop, each cycle requires O (n) time. In addition, we call the issymmetrical in the loop, and the Time of O (n) is required for each invocation. So the time efficiency of the entire function is O (N3).

Usually O (N3) is not an efficient algorithm. If we carefully analyze the comparison process of the above methods, we can see that there are many duplicate comparisons. Suppose we need to determine that a substring is in the form of AAA (A is a AAA substring and may contain more than one character). We point the Pfirst to the first character a, Plast to the last character a, and as the two characters are the same, we move the pfirst backwards in the issymtical function and move the Plast forward to determine if a is symmetric. After a few steps, since a is also a substring of the input string, we need to judge once again that it is not symmetric. In other words, we repeatedly judge whether a is symmetric.

The root cause of this repetition is that the comparison of Issymmetrical is carried out from the outside. When judging whether AAA is symmetrical, we do not know if a is symmetrical and therefore need to spend O (n) time to judge. The next time we judge if a is symmetrical, we still need O (n). Algorithm of Solution two: O (n2)

If we change one way of thinking, we'll judge from inside to outside. That is, we first judge whether substring a is symmetric. If a is not symmetric, the string that extends one character to each end of the substring is certainly not symmetric. If a is symmetric, then we only need to judge whether a character extended at both ends is equal, and if it is equal, the extended string is symmetric. So when you know if a is symmetrical, you only need O (1) Time to know if AAA is symmetrical.

We can write the following code based on the idea of comparing from inside to outside:

///////////////////////////////////////////////////

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.