HDU 3068 longest palindrome manacher algorithm O (n) palindrome substring algorithm

Source: Internet
Author: User
Tags lowercase

http://acm.hdu.edu.cn/showproblem.php?pid=3068
the longest palindrome Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 21482 Accepted Submission (s): 7772 problem Description gives a group of only lowercase English characters a,b,c...y,z To the length of the longest palindrome string in S.
Palindrome is the same as the positive and negative reading is the same string, such as ABA, ABBA input has multiple sets of case, not more than 120 groups, each set of input is a line of lowercase English characters a,b,c...y,z a string s
Between two sets of case separated by a blank line (the empty line is not processed)
String length len <= 110000 Output An integer x for each row, corresponding to a set of case, representing the longest palindrome length contained in the string for the group case. Sample Input

AAAA abab Sample Output
4 3 Test Instructions: Find the longest palindrome in the string and output its length.

Idea: Manacher algorithm O (n) palindrome substring algorithm,

http://blog.csdn.net/xingyeyongheng/article/details/9310555 This blog is very clear, although I was just beginning also a face confused, do not know what he wrote, to learn the algorithm, you'd better emulate the code yourself , the blog has chestnuts, their own hand push it.

http://blog.csdn.net/ggggiqnypgjg/article/details/6645824 This blog is also very good

There is a point at the beginning of the time do not understand, here I want to ask.

For example: String AA (even strings), his symmetry center without letters, string aaa (odd string), his symmetry center has letters. In order to unify processing, so the symbol # is added, so that the odd string pairs have the letters of the center of symmetry.

Original string: W a A B w s W f D

I 1 2 3 4 5 6 7 8 9 10111213141516171819
New string: # W # a # a # b # w # s # w # f # d #
Auxiliary array p:1 2 1 23 2 1 2 1 2 1 4 1 2 1 2 1 2 1

P[id] Represents a palindrome string with a radius (this palindrome includes #), centered on the ID, P[id]

For example: P[5]=3 indicates that the i=5 is the center and has a length of 2*p[5]-1 palindrome string.

The P array has a property, that is, the length of the palindrome string in the original string (centered on the p[i]-1)


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

const int MAX=110000+10;
Char s[max*2];
int p[max*2];

int main ()
{while
    (~scanf ("%s", s)) {
        int len=strlen (s), id=0,maxlen=0;
        for (int i=len;i>=0;--i) {//insert ' # '
            s[i+i+2]=s[i];
            s[i+i+1]= ' # ';
        } Insert a len+1 ' # ', the final s length is 1~len+len+1 i.e. 2*len+1, s[0] and s[2*len+2] to insert different characters
        s[0]= ' * ';//s[0]= ' * ', s[len+len+2]= ' To prevent p[i] out of bounds for
        (int i=2;i<=2*len;++i)
        {
            if (p[id]+id>i) p[i]=min (p[2*id-i],p[id]+id-i) during while ;//This is the algorithm to avoid the core of repetitive computation (key simulation to find the law)
            else p[i]=1;
            while (s[i-p[i]] = = S[i+p[i]]) ++p[i];
            if (Id+p[id]<i+p[i]) id=i;
            if (Maxlen<p[i]) maxlen=p[i];//statistics max
        }
        cout<<maxlen-1<<endl;
    }
    return 0;
}





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.