Hdu 5371 Hotaru ' s problem (manacher+ ruler)

Source: Internet
Author: User

Test instructions

Given a sequence of n numbers, find a continuous sub-sequence that satisfies such a condition:
1. Average divided into three segments
2. The first paragraph is the same as the third paragraph
3. The second paragraph is the reverse of the first paragraph. The maximum length of such subsequence.
Data range: n~100000

Analytical:

I see a lot of the network above the puzzle is to use O( n 2 / +) The practice of water data in the past, this approach is to first use the Mancher algorithm to preprocess each palindrome string farthest can reach the location, and then enumerate each location i , enumerate the length of its palindrome, and then enumerate the current position i To the farthest distance the palindrome can reach, to determine the location of the pathway J Yes, no palindrome can go to the current position i 。 But the complexity of this approach is too high, not a positive solution.

There is a better way to do it:
According to the official:
The above conditions abstract is actually a double palindrome string, so the topic is to find a sequence of the longest double palindrome string.

The specific approach is:

    1. First, the maximum palindrome radius of each element is calculated by using the manacher algorithm O (n).

    2. Each element as a center, then two points can constitute a double palindrome must meet the condition is that they in the other side of the circle or circle (draw a understand), so the next how to use the maximum palindrome radius it.

Specific practices:
  1. You can separate the left interval of each position and the interval of each position.
  2. Put the left interval into the a array and the right interval into the B array.
  3. Sorts A, B, two arrays by the left boundary.
  4. Then the right edge of a can be seen as the center of the circle, and the left edge of B can be seen as the center of the circle.
    Then the qualifying interval is
    < Span class= "Mrow" id= "mathjax-span-26" > a . L <= B L And B.L<= A.R<= B.R
  5. Since the left boundary is sorted to satisfy monotonicity, a ruler can be used to enumerate two intervals and maintain the maximum value.
    The complexity of Manacher is O (2n), the complexity of sequencing is O (NLOGN), the complexity of the ruler is O (n), the total complexity is O(3N+NLog N)

y code

#include <cstdio>#include <cstring>#include <algorithm>Using namespace Std;constintINF =0x3f3f3f3f; constintN =100005;intStr[n * *],s[N], Len;intP[n * *], n;void Manacher () {len = n; for(inti =0; i < Len; i++) {Str[i * *] = INF; Str[i * *+1] =s[i]; } Str[len * *] = INF; Len = len * *+1;intMAXP =0, id =0; for(inti =0; i < Len; i++) {if(Maxp > i) p[i] = min (maxp-i, P[id * *-I.]);ElseP[i] =1; while(Str[i-p[i] [= Str[i+p[i]] && i+p[i] < len && I-p[i] >=0) p[i]++;if(I+p[i] > Maxp)            {MAXP = P[i]+i;        id = i; }}}struct Line {intL, R; Line () {} line (intLintR): L (L), R (r) {}int length() {returnR-l +1;}} A[n], b[n];intAN, Bn;bool CMP (line A, line B) {returnA.l < B.L;} void Prepare () {an = Bn =0; for(inti =0; I < n; i++) {intLen = (p[i * *] -1) >>1;if(len = =0)Continue; a[an++] = line (I-len, I-1);if(i +1< n) b[bn++] = line (i, i + Len-1); }Sort(A, A+an, CMP);Sort(B, B+BN, CMP);}intCal () {intMAXV =0, L =0; for(inti =0; I < bn; i++) { while(L < an && a[l]. L <= B[i]. L) {if(B[i]. L <= A[l]. R && A[l]. R <= B[i]. R) {MAXV = max (MAXV, A[l]. R-b[i]. L +1);        } l++; }    }returnMAXV *3;}intMain () {intT, CAS =1; scanf"%d", &t); while(t--) {scanf ("%d", &n); for(inti =0; I < n; i++) {scanf ("%d", &s[i]);        } manacher (); Prepare ();printf("Case #%d: %d\ n", cas++, Cal ()); }return 0;}

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

Hdu 5371 Hotaru ' s problem (manacher+ ruler)

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.