The longest palindrome substring algorithm

Source: Internet
Author: User

#1032: Maximum palindrome substring time limit:1000msSingle Point time limit:1000msMemory Limit:64MB
Describe

Small hi and small ho is a pair of good friends, born in the information society, they have a great interest in programming, they agreed to help each other, in the programming of learning along the road together.

On this day, they encountered a string of strings, so little Hi to small ho raised the classic question: "Little ho, you can find each of them in these strings each of the longest palindrome string it?" ”

Little Ho asked curiously, "What is the longest palindrome string?" ”

Little hi replied:"a continuous paragraph in a string is the substring of this string, and palindrome string refers to the 12421 of this from the back to read and read from the back to the same string, so the longest palindrome substring is the longest string of a palindrome is the substring ~"

Little Ho Way: "That's it!" So how do I get these strings? What should I tell you about the longest palindrome string I've calculated?

Little hi smiled and said: "This is very easy, you just need to write a program, first read an integer N(n<=30)from the standard input, representing the number of strings I gave you, and then the next is the N string I want to give you (string length <=10^6 ). and you have to tell me your answer, as long as you calculate the length of the longest palindrome string in the order I give you to output to the standard output on it! you see, this is an example. "


Sample input
3abababaaaaabaaacacdas
Sample output
753

Topic Analysis:

The longest palindrome substring, as the name implies, is a substring of a string, and the substring satisfies the requirements of palindrome strings, such as ABBA, ABA and so on.

Ideas:

Consider a palindrome with an odd length, as an even number.

1, enumerating the length of a palindrome substring centered on each character of a string, such as a substring: Aabac, which is centered on the first and fifth characters, and enumerates a palindrome with an odd length. Records the longest palindrome substring length.

2, for Aaaaaaaaaaab, this type of string, because there are a lot of the same characters, so in the process of enumeration, may be a little optimization. The specific optimization ideas are as follows:


, f (i) represents the longest palindrome substring centered on the character I, according to the above information, can infer a[5] = a[3], a[3] = a[1], a[1] = a[7], so a[5] = a[7], so f (6) >= 3, so at the time of enumeration, for the A[7 ] as the center of the palindrome string, only need to consider a[4] = a[8]?, if equal to continue to judge a[3] = a[9]?, if not equal, then the longest palindrome substring at this time can be recorded as the value of f (6).

In fact, using I for 6,J represents 4, so there are:

F (i) >= min{f (2*j-i), F (j) -2 (I-J)}

where F (2*j-i) is the F (2) in the figure, F (J)-2 (I-j) is F (4) can cover the length of F (6)

For the above formula, do some deformation:

f (i) + 2*i >= min{f (2*j-i) + 2 (2*j-i) + 4 (i-j), F (j) + 2*j},i > J

Can see F (2*j-i) + 2 (2*j-i) + 4 (I-J) > F (2*j-i) + 2 (2*j-i), so f (i) + 2*i is incremented, only a J is recorded, so that F (j) + 2*j is the largest, then the minimum value of f (i) can be obtained, in the This information can be used to reduce the amount of computational effect.


For the case of even-length palindrome strings, the basic similarity is:



Finally, the code is attached for analysis:

#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <string    >using namespace std; #define N 1000010class Solution {Public:char buf[n];    int f[n];        void Solve () {int kase;        scanf ("%d", &kase);            while (kase--) {scanf ("%s", &buf);            int longestlength = Longestpalindrome (BUF);        printf ("%d\n", longestlength);        }} int Longestpalindrome (char buf[]) {int n = strlen (BUF);        Int j = 0, I, K;        In case the length of palindrome is odd f[0] = 1;        int result = 1;            for (i = 1; i < n; i++) {F[i] = min (f[2*j-i] + 2*i, f[j]+2*j)-2*i;            if (F[i] < 0) F[i] = 1; for (int left = i-f[i]/2-1, right = i + F[I]/2 + 1; left >= 0, right < N; left--, right++) {if (                Buf[left] = = Buf[right]) f[i] + = 2;            else break; } if (F[i] + 2*i > f[j] + 2*J) j = i;        result = Max (result, f[i]); }//In case the length of palindrome is even f[0] = 0;        j = 0;            for (i = 0; i < n-1; i++) {F[i] = min (f[2*j-i], f[j]-2*i+2*j);            if (F[i] < 0) F[i] = 0; for (int left = I-F[I]/2, right = i + 1 + f[i]/2; left >= 0, right < N; left--, right++) {if (buf                [Left] = = Buf[right]) f[i] + = 2;            else break;            } if (F[i] + 2*i > f[j] + 2*j) j = i;        result = Max (result, f[i]);    } return result;    }};int Main () {solution solution;    Solution.solve (); return 0;}


The longest palindrome substring algorithm

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.