Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5371
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
Test instructions
Given n numbers, to find its contiguous subsequence and sub-sequence to meet the following two conditions:
1, the identified sub-sequence can be divided into 3 parts, the first part and the second part is symmetrical;
2, the first part and the third part are the same!
To find out the maximum length of such a subsequence!
Ps:
Manacher algorithm, using p[i], to enumerate!
The code is as follows:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD; #define MAXN 200017int n;int p[maxn];int a[maxn];int b[maxn];//Convert original string int n;void init () {int i; for (i = 0; i < n; i + +) {b[2 * i + 1] =-1; B[2 * i + 2] = A[i]; } N = 2 * i + 1; B[0] =-2; B[n] = b[n + 1] =-1;} void Manacher () {int id; int Maxx = 0;//maxx is the maximum value of the rightmost character of the currently computed palindrome, int ans = 0; for (int i = 1; I <= N; i + +) {if (Maxx > i) p[i] = min (maxx-i, p[2*id-i]);//In len[j] and mx-i take a small else P[i] = 1;//if i>=mx, start from scratch and match while (B[i + p[i] [= B[i-p[i]]) + + p[i]; if (i + P[i] > Maxx)//Jovin computed palindrome string right endpoint position greater than Maxx, to update ID and maxx value {maxx = i + p[i]; id = i; }}}int Main () {int t; int cas = 0; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (int i = 0; i < n; i++) {scanf ("%d", &amP;a[i]); } init ();//Convert original string Manacher (); int ans = 1;//for (int i = 1; i < N; i++)//{//printf ("%d>>%d\n", B[i],p[i]);/} for (int i = 3, i < N; i+=2) {for (int j = ans; J <= P[i]; j+=2) { if (P[i+j-1] >= j) {ans = j; }}} ans = ans/2*3; printf ("Case #%d:%d\n", ++cas,ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5371 Hotaru ' s problem (Manacher + enumeration AH)