Hdu5371hotaru ' s problem

Source: Internet
Author: User

Test instructions: Given a string that asks for the length of the oldest string, the substring satisfies it to be divided into three parts, the first part and the second part are palindrome strings, the third part is the same as the first part.

Practice:

First, using the Manacher algorithm to find the longest palindrome string, the length of the palindrome string centered on the point I and the i+1 point is obtained, and recorded in the array C for example 10 9 8 8 9 10 10 9 8 We run the Manacher to find the first point and the first i+1 point of the palindrome string length 0 0 6 0 0 6 0) 0 0

Two 8 is the center, 10 9 8 8 9 10 is a palindrome string, the length is 6. Two 10 is the center, 8 9 10 10 9 8 is a palindrome string, the length is 6.

To meet the requirements of the topic, you need to make two adjacent palindrome strings, sharing the middle part, such as the top two strings, sharing 8 9 10 this part. That is, the left side of the palindrome string half of the length, to greater than equal to the length of the shared part, the right palindrome is the same. Because we have recorded the length of the palindrome string centered on the point I and i+1, then the problem can be converted to two numbers a[i],a[i+x "x", satisfying a[i]/2>=x and a[i+x]/2>=x, requiring X to be as large as possible

This can be maintained with a set, the beginning of the collection is empty, the subscript is bound with the value in a array, and then sorted by the value in descending order, then the array of a in the list of elements, the subscript placed in the set, each take out an element, and then the collection of two points less than equals I+A[I]/2, but the largest element, Update answer ans. Then find the answer in the collection that is greater than or equal to I-A[I]/2, but the smallest element that updates ans.

Because after sorting, every time I go in to throw the element, if the elements in the set can be touched by I, then it is certain that the element can touch I.

The answer is 3*ans.

The complexity of time is Nlogn

#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;const int maxn=110010;int ma[maxn*2],mp[maxn*2],s[maxn];void manacher (int Len) {int l=0; Ma[l++]=-1; ma[l++]=-2;for (int i=0;i<len;i++) {ma[l++]=s[i]; Ma[l++]=-2;} 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;}}} struct A{int Id,val;bool operator < (A one) Const{return val!=one.val?val<one.val:id<one.id;} a[maxn];void Create (int len) {manacher (len); len=2*len+2;for (int i=3;i<len;i+=2) {int id=i/2-1;a[id].id=id;a[id]. Val=mp[i]-1;}} int work (int len) {int ans=0;sort (a,a+len); set<int>st;set<int>::iterator it;for (int i=len-1;i>-1;i--) {if (a[i].val==0) Break;int t=a[i].id+a[i].val/2;it=st.upper_bound (t); if (it!= St.begin ()) {it--;if (*it<=t&&*it>a[i].id) Ans=max (ans,*it-a[i].id);} T=a[i].id-a[i].val/2;it=st.lower_bound (t); if (It!=st.end () &&*it<a[i].id) Ans=max (ans,a[i].id-*it); St.insert (a[i].id);} return ans;} int main () {int t;scanf ("%d", &t), for (int cs=1;cs<=t;cs++) {int n;scanf ("%d", &n), and for (int i=0;i<n;i++) scanf ("%d", s+i), create (n);p rintf ("Case #%d:%d\n", Cs,3*work (n));} return 0;}

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


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 than Ten 9 , 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 Input
1102 3 4 4 3 2 2 3 4 4

Sample Output
Case #1:9

Source2015 multi-university Training Contest 7

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

Hdu5371hotaru ' s 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.