HDU 5371--hotaru ' s problem —————— "Manacher treatment palindrome"

Source: Internet
Author: User

Hotaru ' s problem

Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1765 Accepted Submission (s): 635


Problem Descriptionhotaru Ichijou recently is addicated to math problems. Now she's playing with N-sequence.
Let's define N-sequence, which is composed with three parts and satisfied with the following condition:
1. The first part of the same as the thrid part,
2. The first part and the second part is symmetrical.
For example, the sequence 2,3,4,4,3,2,2,3,4 are a n-sequence, which the first part 2,3,4 are the same as the Thrid Part 2, 3, 4, the first part 2,3,4 and the second part 4,3,2 is symmetrical.

Give you n positive intergers, your task was to find the largest continuous sub-sequence, which is n-sequence.

Inputthere is multiple test cases. The first line of input contains an integer T (t<=20), indicating the number of test cases.

For each test case:

The first line of input contains a positive integer N (1<=n<=100000), the length of a given sequence

The second line includes N non-negative integers, each interger is no larger than9 , descripting a sequence.

Outputeach case contains only one line. Should start with ' case #i: ', with I implying the case number, followed by a integer, the largest length of n-se Quence.

We guarantee the sum of all answers are less than 800000.

Sample Input1102 3 4 4 3 2 2 3 4 4

Sample outputcase #1:9  topic: give you T Group of data, an n, the following line has n number, ask you can form like abccbaabc such a sequence length of the longest is how much.   Problem Solving ideas: first with manacher processing out the string of all the characters of the palindrome radius. Then enumerate the first paragraph and the second section of the palindrome position I, from I+p[i]-->i to enumerate the second paragraph and the third section palindrome position J. If J is the center of the left end can be less than equal to the position of I, to meet the requirements, update the results.  
#include <bits/stdc++.h>using namespace std; #define MIN (a) < (b) (    A):(B)) const int Maxn=1e6;int a[maxn],p[maxn];void manacher (int n) {a[0]=-2;a[n+1]=-1;a[n+2]=-3;    int mx=0,id=0;   for (int i=1;i<=n+1;i++) {//needs to be processed to n+1 if (i<mx) {p[i]=min (p[id*2-i],mx-i);        It was written in MX-ID,SB.        }else{p[i]=1;  } for (; a[i+p[i]]==a[i-p[i]];++p[i]);            -2,-3 Cross-border if (i+p[i]>mx) {mx=p[i]+i;        Id=i;    }} for (int i=1;i<=n+1;++i) {--p[i];    }}int Main () {//Freopen ("1003.in", "R", stdin);//Freopen ("OUTTTT.txt", "w", stdout);    int t,n,cnt=0;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);            for (int i=1;i<=2*n;i+=2) {a[i]=-1;        scanf ("%d", &a[i+1]);        } manacher (2*n);        int maxv=0,j; for (int i=1;i<=2*n;i+=2) {//2*n for (j=i+p[i];j-maxv>i;j-=2) {//Reverse enumeration.              Manacher algorithm guarantees j<=2*n+1   if (j-p[j]<=i) {maxv=j-i;                Break        }}} maxv=3* (MAXV/2);    printf ("Case #%d:%d\n", ++CNT,MAXV); } return 0;}

  

HDU 5371--hotaru ' s problem —————— "Manacher treatment palindrome"

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.