3 solutions for the length of the maximum symmetric substring in a C-language output string _c language

Source: Internet
Author: User

Problem Description:

Enter a string that outputs the length of the largest symmetric substring in the string. For example, the input string: "Avvbeeb", the longest substring in the string is "Beeb", the length is 4, and thus the output is 4.

WORKAROUND: In-sequence traversal

One, full traversal of the method:

1. Full traversal method, complexity O (N3);

2. Iterate through all substrings of the original string, and then determine whether each substring is symmetric;

The implementation method is: We let a pointer I traverse through, we use another pointer J from J=i+1 to point to all the characters after I. It realizes the traversal of all substrings of the original string (the substring is the part of the pointer I to the middle of j).
Finally, it is possible to judge whether the obtained substring is symmetrical.

Second, there is also a clever way, it is worth sharing with you (this is what you think oh, reproduced please indicate the source):

The original string is str1= "Avvbeeb", flip it to get str2= "Beebvva" and then dislocation comparison:

1:avvbeeb

Str2:beebvva (the top and bottom aligned elements are a;a comparisons)

2:avvbeeb

Str2:beebvva (Av;va, asymmetric) of the volume elements of the top and bottom alignment

............

11:avvbeeb

Str2:beebvva (The Beeb;beeb of the upper and lower elements is compared to get the longest symmetric substring)

............

The method is to move m+n times, with each element comparing the number from 1 to M, the complexity O (N2);

Third, the most recommended or the following method, complexity O (n):

(The following are their own to write, code word really hard, reprinted please indicate the source)

1. The beginning of this problem analysis is very nonsense, it took me two days of free time to finish!

2. The analysis process is as follows:

3. In the element of 1-k bit, where the longest symmetric substring (including the K-bit element) is f (n), we discuss the relationship between F (n+1) and f (n);

4. For example, b xxx A, where xxx represents the symmetric substring, A is the first n+1 bit element, we now seek f (n+1);

5. We analyze all cases: (we use XXX to represent n-bit symmetric substrings)

Array a holds the array of characters;

F (n) indicates the length of the corresponding substring of the f (n) bit element;

Analyze the values of the substring length value f (n+1) for the following a[n+1]=a:

1:bxxxa:a[n+1] Bit element A is different from a bit element B before the symmetric sub xxx string;

1.1:a is different from the left adjacent element, i.e. XXX=BXB, BBXBA is not a symmetric substring, f (n+1) = 1;

1.2:a is the same as the left adjacent element, that is, when the Xxx=axa, BAXAA, if it is a symmetric substring, then x this unknown part must be all a, that is

Baaaa,f (n+1) =f (n) +1, otherwise it is not a symmetric substring f (n+1) = 1;

Axxxa:a[n+1] Bit element A is the same as the previous element of the symmetric substring;

2. In this case, whether F (n+1) bit element A is the same as its left adjacent element does not affect the result of f (n+1),

For example: A Bacab a aaaaa a

String Length: 1 13135 7 1 23456 7 is xxx regardless of the situation of the symmetric string, F (n+1) =f (n) +2;

6. In comprehensive analysis, the value F (n+1) of the string a[n+1] is related only to the a[n] bit character in the string and to the A[n-f (n)-1].

(5 The analysis of the F (n+1) =1 can be omitted without consideration, because the minimum symmetric substring value >=1)

1:A[N+1] is the same as A[N-F (n)-1];

A xxx x A:acca aaaa ACDCA

A[n-f (n)-1] a[n] a[n+1]

F (N) F (n+1): 1124 1234 11134

At this time F (n+1) =f (n) +2;

2:a[n+1] and A[n-f (n)-1] are different; A[N+1] and a[n] the same;

such as: b xxx a A:BCACAA baaaaa

A[n-f (n)-1] a[n] a[n+1]: 111332 112345

At this time F (n+1) with its front has several a concerned;

Comprehensive analysis of the code is as follows:

Copy Code code as follows:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int FUN (char *inp) {//MAX symmetric substring length
int maxlen = 1;//Maximum length
int Len=strlen (INP);
int record[len];//contains the longest symmetric substring of the bit and the previous element
Record[0]=1;
int i=1;
for (; i<len;i++) {
int max = 1;
if ((i-record[i-1]-1) >=0 && inp[i] = = Inp[i-record[i-1]-1]) {
max = max> (Record[i-1] + 2)? Max: (Record[i-1] +2);
}
int k = 1;
while (inp[i] = = Inp[i-k]) {
k++;
}
max = max>k? Max:k;
Record[i] = max;
printf ("-----is:%d\n", record[i]);
if (Record[i]>maxlen) maxlen=record[i];
}
return maxlen;
}

int main () {
Char *input= "ABADDDKEIPDLDLFK";
int retlen = FUN (input);//Previous backward recursion
printf ("Max length is:%d\n", Retlen);
return 0;
}

Output results:

Copy Code code as follows:

xu@xu-thinkpad-x61:~/algorithm$ GCC LONGSUNMETRICSUB.C
xu@xu-thinkpad-x61:~/algorithm$./a.out
-----is:1
-----is:3
-----is:1
-----is:2
-----is:3
-----is:1
-----is:1
-----is:1
-----is:1
-----is:1
-----is:1
-----is:3
-----is:1
-----is:1
-----is:1
Max length Is:3

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.