Hotaru ' s problem Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 243 Accepted Submission (s): 61
Problem Description Hotaru 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.
Input There 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 109, descripting a sequence.
Output Each 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
1 10 2 3 4 4 3 2 2 3 4 4
Sample Output
Case #1:9
Source multi-university Training Contest 7
The longest palindrome substring of each split position is calculated using the Manacher algorithm.
Then enumerate each split position
The answer string from the analysis topic is
ab|ba| AB is a concatenation of two palindrome strings, with two symmetry centers
For each position I, palindrome string length len[i], in [I,i+len[i]] to find the largest J that satisfies J-len[j] <= i.
Then the resulting group of Solutions is min (Len[i], j-i)
Since the Manacher algorithm inserts a symbol at each location, all only computes the even digits, and the even digit is the split position.
The maximum solution of searching interval can be maintained by using segment tree.
The node of the segment tree represents the location of the j-len[j]
#include <cstring> #include <cstdio> #include <string> #include <iostream> #include <
Algorithm> using namespace std;
#define MAXN 800007 int LC[MAXN],RC[MAXN],VAL[MAXN];
int RAD[MAXN];
int TMP[MAXN];
int S[MAXN];
int inf = 1000000000;
void Manacher (int n) {//find palindrome string int cnt = 0;
memset (Rad,0,sizeof (RAD));
for (int i=0;i<n;i++) {s[cnt++] = inf;
s[cnt++] = Tmp[i];
} s[cnt++] = inf;
n = cnt;
int i=0,j=1,k;
while (I<n) {while (i-j>=0 && i+j<n && s[i-j]==s[i+j]) j + +;
Rad[i]=j-1;
k=1;
while (K<=rad[i] && rad[i]-k!=rad[i-k]) {rad[i+k]=min (rad[i-k],rad[i]-k);
k++;
} i+=k;
J=max (j-k,0);
}} int tcnt;
Achievement, the odd digit is the position of the original string, cannot be used as the partition point void build (int u,int l,int R) {if (L = = r) {if (& 1) val[u] = inf;
else val[u] = l-rad[l];
return;
} Lc[u] = tcnt++; Rc[u] = TCNt++;
int mid = (l+r)/2;
Build (Lc[u],l,mid);
Build (Rc[u],mid+1,r);
Val[u] = min (Val[lc[u]],val[rc[u]]);
}//The query satisfies the rightmost position of less than or equal to p in the L,r interval int query (int u,int l,int r,int l,int r, int p) {if (L = = r) return l;
int mid = (l+r)/2;
if (L = = L && r = = r) {if (Val[u] > P) return inf;
if (Val[rc[u]] <= p) return query (RC[U],MID+1,R,MID+1,R,P);
else if (Val[lc[u]] <= p) return query (LC[U],L,MID,L,MID,P);
else return INF;
} if (Mid < L) return query (RC[U],MID+1,R,L,R,P);
else if (mid >= R) return query (LC[U],L,MID,L,R,P);
else {int ans = query (rc[u],mid+1,r,mid+1,r,p);
if (ans! = inf) return ans;
return query (LC[U],L,MID,L,MID,P);
} return INF;
} void Init () {tcnt = 2;
memset (rc,0,sizeof (RC));
Memset (Lc,0,sizeof (LC));
Memset (Val,0x3f,sizeof (Val));
} int main () {int t,tt=1,n;
scanf ("%d", &t); while (t--) {scanf ("%d", &n);
for (int i = 0;i < n; i++) scanf ("%d", &tmp[i]);
Manacher (n);
n = n*2+1;
Init ();
Build (1,0,n-1);
int ans = 0;
for (int i = 0;i < n; i+=2) {//enum split position int p = i + rad[i];
int q = query (1,0,n-1,i,p,i);
if (q = = inf) continue;
int res = min (rad[i],q-i);
ans = max (ANS,RES/2);//manacher length increased by one times, all to/2} printf ("Case #%d:%d\n", tt++,ans*3);
} return 0;
}/* 22 10 2 3 4 4 3 2 2 3 4 4 20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 1 1 2 2 2 2 1 1 1 1 2 2 *