! HDU 5371 longest double palindrome (multi-school 7)-card time-(manacher+ sort +set+lower_bound ())

Source: Internet
Author: User
Tags cas

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

Analysis:

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 main practice is:

1. First use the manacher algorithm O (n) to find the maximum palindrome radius of each element;

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. The best way to do this: first by the descending order of the radius, and then put the center coordinates in the set inside, before a center A is put in the set to find in the center of the circle in the circles or around the circle of the farthest two center (with Lower_bound () two points to achieve. This step is to find a double palindrome at each center point for a inflection point can constitute the longest double palindrome string, because the radius of the first into the set is certainly greater than the radius of the current center, then must meet the current center a must be in the set of the circle inside or round, So just find out if the set is in circle A or round a, and then update ans on the line. That's wonderful!

Code:

#include <cstdio> #include <cstring> #include <iostream> #include <set> #include <algorithm >using namespace std; int t,n;int a[200015];struct node{int x, y;}; Node p[200015];set<int> s;set<int>::iterator it;bool cmp (node A,node b) {return a.y>b.y;}    void Rx (int n) {memset (p,0,sizeof (p));    for (int i=0;i<n;i++) p[i].x=i;    int id=0,mx=0;        for (int i=0;i<n;i++) {if (mx>i) p[i].y=min (p[2*id-i].y,mx-i);        else P[i].y=1;        while (a[i-p[i].y]==a[i+p[i].y]&&i-p[i].y>=0&&i+p[i].y<n) p[i].y++;            if (mx<i+p[i].y) {mx=i+p[i].y;        Id=i;    }}}int Main () {scanf ("%d", &t);        for (int cas=1;cas<=t;cas++) {s.clear ();        int ans=-1;        scanf ("%d", &n);        A[0]=-1;            for (int i=1;i<2*n;i+=2) {scanf ("%d", &a[i]);        A[i+1]=-1;        } int len=2*n+1;        Rx (len); for (int i=0;i<=n;i++) {p[i]=P[I*2];        } sort (p,p+n+1,cmp);        S.insert (p[0].x);            for (int i=1;i<=n;i++) {int tmp=p[i].x+p[i].y;            It=s.lower_bound (TMP);                if (It==s.end ()) {--it;                if ((*it) >p[i].x) Ans=max (ans, (*it)-p[i].x);//Before the cause of the error, do not understand the subscript relationship} else if ((*it) ==tmp) {            Ans=max (ans, (*it)-p[i].x);                } else if (It!=s.begin ()) {--it;            if ((*it) >p[i].x) Ans=max (ans, (*it)-p[i].x);            } tmp=p[i].x-p[i].y;            It=s.lower_bound (TMP);            if (It!=s.end ()) {if ((*it) <p[i].x) Ans=max (ans,p[i].x-(*it));        } s.insert (p[i].x);    } printf ("Case #%d:%d\n", cas,ans/2*3); }}


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

! HDU 5371 longest double palindrome (multi-school 7)-card time-(manacher+ sort +set+lower_bound ())

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.