[Algorithm 25] maximum length of a symmetric substring

Source: Internet
Author: User

[Topic] enter a string and output the maximum length of the symmetric substring in the string. For example, input string: "google". The longest substring in the string is "Goog" and the length is 4, so the output is 4.

[Thinking Road 1] reading this question is traversing! That's right. What we use most intuitively is often the easiest way to implement. Here we do not consider efficiency for the moment. Our basic idea is: if we have a function that determines whether a string is symmetric, we can use this subfunction to check all the strings in the original string one by one, then the maximum output length is enough.

(1) how to determine whether a string is a symmetric string? We can use two pointers to point to the first character and the last character of the string respectively to determine whether they are equal. If not, false is returned directly. If true, the next character is compared. (2) If we traverse all the substrings of the original string, first let a pointer traverse from the beginning to the end. For each character of this pointer, we can use another pointer to point to each character next to it one by one. Okay, we have solved both of the problems. We can write the following:Code:

 1 # Include <iostream>
2 # Include < String >
3 # Include <cstring>
4 Using Namespace STD;
5
6 /* **************************************** *******
7 * Determine whether a string is symmetric.
8 **************************************** ******** */
9 Bool Issymmetricalstring ( Char * Pstart, Char * Pend)
10 {
11 If (Pstart = NULL | pend = NULL | pstart> pend)
12 Return False ;
13
14 While (Pstart <pend)
15 {
16 If (* Pstart! = * Pend)
17 {
18 Return False ;
19 }
20 Else
21 {
22 Pstart ++;
23 Pend --;
24 }
25 }
26
27 Return True ;
28 }
29
30
31 /* **************************************** ********
32 * Obtain the maximum length of the symmetric substring.
33 **************************************** ********* */
34 Int Maxcompute ricalsubstringlenth ( Char * Pstring)
35 {
36 If (Pstring = NULL)
37 Return 0 ;
38
39 Int Maxlength = 1 ;
40
41 Int Length = strlen (pstring );
42 Char * Pfirst = pstring;
43 While (Pfirst <& pstring [length- 1 ])
44 {
45 Char * Required cond = pfirst + 1 ;
46 While (Required cond <= & pstring [length- 1 ])
47 {
48 If (Isw.ricalstring (pfirst, 1_cond ))
49 {
50 Int Templength = required cond-pfirst + 1 ;
51 If (Templength> maxlength)
52 {
53 Maxlength = templength;
54 }
55 }
56
57 Consumer cond ++;
58 }
59
60 Pfirst ++;
61 }
62
63 Return Maxlength;
64 }
65
66 Int Main ()
67 {
68 Cout <" Please enter your string (the length must <1000 ): " <Endl;
69 Char * Yourstring = New Char [ 1000 ];
70 Cin> yourstring;
71
72 Cout < " The max parameter rical substring length in your string is: " <Endl;
73 Cout <maxcompute ricalsubstringlenth (yourstring) <Endl;
74
75 Return 0 ;
76 }

The running result is as follows:

Reflection: Let's analyze it now.AlgorithmFirst, there are two cycles for traversing all the substrings of the original string. the time complexity is O (n2 ), we have a subfunction between these two loops to determine whether a given string is symmetric. the time complexity of this subfunction is O (n), so the total time complexity is O (N3 ). since we determine whether a string is symmetric from the external to the internal, we can determine whether AA is equal for the string that is in the form of ABA (B can contain many characters, we have to judge whether B is symmetric, but we have to judge it again when determining the substring B. In other words, we have repeated judgments on many substrings. Can we eliminate this duplication? ,

[Thinking 2] According to the above analysis, we can easily think that if we compare characters from inside to outside, then for ABA strings, If we determine that B is symmetric, you only need to move one character between the left and right to determine whether the next string is symmetric, so that we can avoid duplication. However, we need to note that, there are two cases for each character in the original string. One is that the substrings are symmetric distributed in the center of a single character. In other words, the length of the substring is an odd number; another case is that the substring is centered on two strings, that is, the length of the substring is an even number. Now we can write the following code:

 1 # Include <iostream>
2 # Include < String >
3 Using Namespace STD;
4
5 /* **************************************** ****************************
6 * Calculate the maximum length of the symmetric substring of a string.
7 **************************************** **************************** */
8 Int Maxcompute ricalsubstringlenth ( Char * Pstring)
9 {
10 Int Maxlength = 1 ;
11
12 Char * Pchar = pstring + 1 ;
13 While (* Pchar! = ' \ 0 ' )
14 {
15 // The string is centered and the length of the substring is odd.
16 Char * Pfirst = pchar- 1 ;
17 Char * Required cond = pchar + 1 ;
18 While (Pfirst> pstring & prop cond <& pstring [strlen (pstring )- 1 ] & * Pfirst = * condition Cond)
19 {
20 Pfirst --;
21 Consumer cond ++;
22 }
23
24 Int Templength = required cond-pfirst + 1 ;
25 If (Templength> maxlength)
26 {
27 Maxlength = templength;
28 }
29
30
31 // This character and the subsequent characters are centered and the substrings are even
32 Pfirst = pchar- 1 ;
33 Required cond = pchar;
34 While (Pfirst> pstring & prop cond <& pstring [strlen (pstring )- 1 ] & * Pfirst = * condition Cond)
35 {
36 Pfirst --;
37 Consumer cond ++;
38 }
39
40 Templength = required cond-pfirst + 1 ;
41 If (Templength> maxlength)
42 {
43 Maxlength = templength;
44 }
45
46 Pchar ++;
47 }
48
49 Return Maxlength;
50 }
51
52 Int Main ()
53 {
54 Cout < " Please enter your string: " <Endl;
55 Char * Yourstring = New Char [1000 ];
56 Cin> yourstring;
57
58 Cout < " The max parameter rical substring length is: " <Endl;
59 Cout <maxcompute ricalsubstringlenth (yourstring) <Endl;
60
61 Delete [] yourstring;
62 Return 0 ;
63 }

The test results are as follows:

Efficiency Analysis: This algorithm first needs to traverse one side of the string with the outer layer of N. For each character, it needs to traverse from the middle to the two sides, therefore, the total time complexity is O (n2 ).

 

References:

ProgramMember interview questions featured 100 questions: http://zhedahht.blog.163.com/blog/static/25411174201063105120425/

Note:

1) Compile all the code environments in this blogWin7 + vc6. All code has been debugged by the blogger.

2 ) blog python27 Article is copyrighted, for Network reprint, please indicate the source http://www.cnblogs.com/python27/ . You have any suggestions for solving the problem. Please feel free to comment on them.

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.