Hihocoder 1032: Longest palindrome string (manacher)

Source: Internet
Author: User

Before doing a similar question, just understand, have not reached the very familiar, think that is the point of knocking, so again practice.
Incidentally, the manacher algorithm thought to explain again, strengthen the impression, also counted as sharing it.

Manacher

We use f (x) to indicate the length of a palindrome centered on the x position.
J Relative position of I is J '
So what does F (j) have to do with F (j ') and F (i)?

First look at the first picture, the following bar represents F (i), then, since J ' and J corresponds to, J ' palindrome string length has been calculated, then J position of the palindrome string length must be greater than or equal to J ' length.
i.e. F (i) >= f (J ') =f (I*2-J)

However, there is a case that the palindrome of I does not completely cover J ' Palindrome string, so the corresponding relationship between J and J ' Palindrome can only be established within the range of I palindrome string.
So, since then, F (i) >= min (f (j '), F (i)-(i-j) * *).

With this conclusion, we can reduce a lot of unnecessary calculation, so as to achieve efficient solution of the longest palindrome substring.

Topic Link: 1032: longest palindrome substring

Ideas

Manacher thought, which has been described above, but that can only solve the longest palindrome is an odd number of cases, for even numbers obviously does not apply.
In fact, slightly flexible.
For example: the longest palindrome string length of Ababa is 5
The longest palindrome length of #a#b#a#b#a# is 11.
It is easy to see that the two are multiplied by the relationship of plus one
For any string of length n, it is interspersed between # and becomes a new string of length 2n+1, which is obviously odd.
The problem is solved.

Code
#include <stdio.h>#include <iostream>#include <string.h>#include <stdlib.h>#include <algorithm>using namespace STD;Charstr[1000009];Chart[2000009];intcnt[1000009];intManacher (CharS[],intLen) {cnt[0] = cnt[1] =1;intID =1;intAns =1; for(intI=2; i<=len; i++) {intnum = min (cnt[id*2-i], cnt[id]+id-i); while(S[i-num] = = S[i+num]) num++; Cnt[i] = num;if(Id+cnt[id] < i+num) id = i;if(Ans < num) ans = num; }returnans-1;}intMain () {intNscanf("%d", &n); while(n--) {scanf('%s ', str); t[0] =' $ ';intLen =strlen(str); for(intI=0; i<len; i++) {t[i*2+2] = Str[i]; t[i*2+1] =' # '; } t[len*2+1] =' # '; t[len*2+2] =' + ';printf("%d\n", Manacher (T, len*2+1)); }return 0;}

Hihocoder 1032: Longest palindrome string (manacher)

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.