HDU 3068 longest palindrome (Manacher)

Source: Internet
Author: User

topic link : [Kuangbin take you to fly] Special topic 16 KMP & Extension KMP & Manacher

Test instructions

A string s consisting of a lowercase English character a,b,c...y,z is given, and the length of the longest palindrome in S is obtained.
Palindrome is the same as the inverse of the string, such as ABA, ABBA, etc.

Ideas

With a special character inserted into the S string in the middle of every two characters, the implementation of each palindrome string is an odd number, and then the manacher algorithm to solve.

Code
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <vector>using namespace STD;Const intN =110009;CharS[n], t[n<<1];intp[n<<1];intManacher (intLen) {intID =1, mx=0; p[0] =1; for(intI=1; i<len; i++) {if(mx > i) p[i] = min (mx-i, p[2*id-i]);ElseP[i] =1; while(T[i+p[i]] = = T[i-p[i]]) p[i]++;if(MX < p[i]+i)            {mx = p[i]+i;        id = i; }    }intAns = p[1]; for(intI=1; i<len; i++)if(Ans < p[i]) ans = p[i];returnans-1;}intMain () { while(~scanf('%s ', s)) {t[0] =' @ '; t[1] =' # ';intLen =strlen(s); for(intI=0; i<len; i++) {t[i*2+2] = S[i]; s[n*2+3] =' # '; }printf("%d\n", Manacher (len*2+2)); }return 0;}

HDU 3068 longest palindrome (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.