Comment on finding the longest palindrome in a string

Source: Internet
Author: User

Class Solution {Public:string Longestpalindrome (string s) {string post;    Post.resize (S.size () *2+2);    Post[0] = ' & ';      for (int i = 0;i<s.size (); i++) {post[2*i + 1] = s[i];    Post[2*i + 2] = ' # ';    } post[s.size () * * = ' # ';    Post[s.size () *2+1]= ' $ ';    cout << "Post:" << post.length () << Endl;    Vector<int> Rad;    Rad.resize (Post.size ());    Rad[0] = 0;    int Max_rad = 0;    int center = 1;    int j = 0;       for (int i = 1; i < Post.length ()-1; i) {while (post[i + j] = Post[i-j]) {j + +;       } Rad[i] = j-1;       cout << i << "," << rad[i];        if (rad[i]> max_rad) {Center = i;      Max_rad = Rad[i];      } int k;        for (k = 1;k < J && Rad[i-k]! = rad[i]-k;k++) {Rad[i+k] = min (Rad[i-k], Rad[i]-K);       cout << i+k << "," << rad[i+k];      } i = i + K;    j = Max (0, j-k);    } string Res; if (center%2 == 1) {res = S.SUBSTR (CENTER/2-MAX_RAD/2, max_rad/2*2+1);    } else {res = S.SUBSTR (center + 1)/2-(max_rad+1)/2, (max_rad+1)/2*2);  } return res; }};


The solution of the problem is actually the manacher algorithm, which is said to be the complexity of O (n), which is characterized by the use of old information in the new palindrome decision.

Problem solving:

1 duality problem, there are actually two dual forms, one is character-centric, and the other is centered on the middle position of two adjacent characters. By inserting # in the middle of the character to turn the problem into the first duality, note that the last character must be inserted in # so that it is guaranteed to be correct, because if it is #b#b, this pattern, in fact, is not equivalent to the original duality problem.

2 using the historical information of duality

The historical information of duality illustrates the precondition of the duality problem to be judged. When Rad[i-k]!=rad[i]-K, indicating I position the longest palindrome, including the i+k position of the longest palindrome, the specific analysis can refer to the longest manacher algorithm, the i+k position of the longest palindrome directly without calculation. When Rad[i-k]=rad[i]-k, the i+k position of the palindrome length at least accumulate rad[i]-k size, so you can directly from the size of the rad[i]-k to start probing.

Essentially, the nature of the palindrome duality is used in the iterative process.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Comment on finding the longest palindrome in a string

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.