HDU 3068 Longest Palindrome "Manacher to find the longest palindrome string, template Problem"

Source: Internet
Author: User

Welcome to __xiong's Blog: http://blog.csdn.net/acmore_xiong?viewmode=list

The longest palindrome Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 11760 Accepted Submission (s): 4308
Link: Click me!
Problem description gives a string s that consists only of lowercase English characters a,b,c...y,z, seeking the length of the longest palindrome 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
Aaaaabab
Sample Output
43
Analysis:

To solve the longest palindrome string, I used to do with DP, in this topic, O (n^2) complexity is sure to be timed out, so I first knocked on a manacher template today, very convenient, complexity also reduced an order of magnitude, the complexity of only O (N), Orz ...

Implementation code:

#include <map> #include <cmath> #include <queue> #include <vector> #include <cstdio># Include <string> #include <cstring> #include <sstream> #include <iostream> #include < algorithm>using namespace std; #define FIN freopen ("Input.txt", "R", stdin) #define FOUT freopen ("ou Tput.txt "," w ", stdout) #define CASE (T) int t;for (scanf ("%d ", &t);  t--;) const int MAXN = 110000 + 5;char ma[maxn << 1];int mp[maxn << 1];void manacher (char s[], int len) {int    L = 0;    ma[l++] = ' $ ';    ma[l++] = ' # ';        for (int i = 0; i < len; i++) {ma[l++] = s[i];    ma[l++] = ' # ';    } Ma[l] = 0;    int mx = 0, id = 0;        for (int i = 0; i < L; i++) {mp[i] = mx > I min (mp[2 * id-i], mx-i): 1;        while (Ma[i + mp[i]] = = Ma[i-mp[i]]) mp[i]++;            if (i + mp[i] > mx) {mx = i + mp[i];        id = i; }}}char S[maxn];int Main () {#ifndef Online_judGE FIN; #endif//Online_judge while (~scanf ("%s", s)) {int len = strlen (s);        Manacher (S, Len);        int ans = 0;        for (int i = 0; i < 2 * len + 2; i++) {ans = max (ans, mp[i]-1);    } printf ("%d\n", ans); } return 0;}

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

HDU 3068 Longest Palindrome "Manacher to find the longest palindrome string, template Problem"

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.