Manacher Algorithm Introduction
Firstly, the Manacher algorithm of palindrome string is used to find the length of palindrome string centered on the first and the i+1 points, and recorded in array C such as 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, at the beginning of the collection is empty, then take the largest element in the a array, put its subscript into the set, each out of an element, then the collection of two points to find <= I+A[I]/2, but the largest element, update the answer. Then find the >= I-A[I]/2 in the collection, but the smallest element, update the answer.
The answer is 3*an.
Hotaru ' s problemTime
limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1384 Accepted Submission (s): 498
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
#include <bits/stdc++.h>using namespace std; #define PRT (k) cerr<< #k "=" <<k<<endltypedef long Long Ll;const ll inf = 0x3f3f3f3f;const int N = 101000;int str[n],ans[n<<1];int p[n<<1],pos,how;int n;void Man Acher () {pos=-1;how=0; memset (P,0,sizeof (p)); int len=2*n+2; int mid=-1,mx=-1; for (int i=0;i<len;i++) {int j=-1; if (i<mx) {j=2*mid-i; P[i]=min (p[j],mx-i); } else p[i]=1; while (I+p[i]<len&&ans[i+p[i]]==ans[i-p[i]]) {p[i]++; } if (p[i]+i>mx) {mx=p[i]+i; mid=i; } if (p[i]>how) {how=p[i]; pos=i; }}}void Pre () {memset (ans,0,sizeof ans); Ans[0] =-1; ANS[1] =-2; for (int i=0;i<n;i++) {ans[2*i+2] = Str[i]; ANS[2*I+3] =-2; } ans[2*n+2] = 0; Manacher (); for (int i=3;i<2*n+2;i+=2) {p[(i-3)/2] = (P[i]-1)/2; }}struct p{int A, id;}; P A[n<<1];bool CMP (p A, p b) {return a.a > B.A;} int main () {int re; scanf ("%d", &re); int CA = 1; while (re--) {scanf ("%d", &n); for (int i=0;i<n;i++) scanf ("%d", &str[i]); printf ("Case #%d:", ca++); if (n < 3) {puts ("0"); Continue } pre (); int ans = 0; for (int i=0;i<n;i++) {//printf ("p[%d] =%d\n", I, p[i]); A[I].A = P[i]; A[i].id = i; } set<int> se; Sort (A, a+n, CMP); for (int i=0;i<n;i++) {Auto it = Se.upper_bound (A[I].ID+A[I].A); if (It! = Se.begin ()) {it--; if (*it-a[i].id <= a[i].a); ans = max (ans, *it-a[i].id); } it = Se.lower_bound (A[I].ID-A[I].A); if (It! = Se.end ()) {ans = max (ans, a[i].id-*it); } Se.insert(a[i].id); } printf ("%d\n", 3*ans); } return 0;} /**34910 9 8 8 9 10 10 9 8*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5371 Hotaru ' s problem