Manacher algorithms Seehttp://blog.csdn.net/u014664226/article/details/47428293
Test instructions: Give a sequence, let the maximal subsequence, this subsequence consists of three paragraphs, the first and second symmetry, the first and third paragraphs.
Idea: First use the manacher algorithm to find the length of a palindrome string centered on any two adjacent elements, using a[i] to denote i-1,i as the center of the palindrome string length of half,
Then the problem is turned into the biggest x, making a[i]>=x,a[i+x]>=x, this step can be greedy to do.
A[i] from the big to the small sort (the indirect sort retains subscript), set A[i] in the original sequence subscript is J,
Each time J is placed in the set, and then two points find the maximum value of the minimum and subscript less than or equal to j+a[i] of the subscript greater than or equal to J-a[i],
So you can find the ANS.
</pre><pre class= "cpp" name= "code" > #include <cstdio> #include <cstring> #include <cmath > #include <cstdlib> #include <iostream> #include <algorithm> #include <vector> #include < map> #include <queue> #include <stack> #include <string> #include <map> #include <set># Define EPS 1e-6#define LL long long#define PII (Pair<int, int>)//#pragma comment (linker, "/stack : 1024000000,1024000000 ") using namespace Std;const int maxn = 2*100000+1000;const int INF = 0x3f3f3f3f;int S[maxn];int p[ma XN], N, A[MAXN], r[maxn];bool cmp (int t1, int t2) {return a[t1] > A[t2];} Set<int> set;struct manacher {void solve () {int len=n,id=0,maxlen=0; for (int i=len;i>=0;--i) {//insert ' # ' s[i+i+2]=s[i]; S[i+i+1]=-1; } s[0]=-2; for (int i=2;i<2*len+1;++i) {if (p[id]+id>i) p[i]=min (p[2*id-i],p[id]+id-i); else p[i]=1; while (s[i-p[i] [= S[i+p[i]]) ++p[i]; if (Id+p[id]<i+p[i]) id=i; if (Maxlen<p[i]) maxlen=p[i]; }}} Manacher;int main () {//freopen ("Input.txt", "R", stdin); int T; Cin >> T; int kase = 0; while (t--) {scanf ("%d", &n); for (int i = 0; i < n; i++) scanf ("%d", &s[i]); S[n] =-3; Manacher.solve (); int cnt = 0; for (int i = 3; I <= 2*n; i + = 2) {a[++cnt] = (p[i]-1)/2; R[CNT] = cnt; }//for (int i = 1; I <= cnt; i++) cout << a[i] << Endl; Sort (r+1, r+cnt+1, CMP); Set.clear (); int ans = 0; for (int i = 1; I <= cnt; i++) {Set.insert (r[i]); Set<int>::iterator T1 = Set.lower_bound (R[i]-a[r[i]]); Set<int>::iterator t2 =--set.upper_bound (R[i]+a[r[i]]); ans = max (ans, Max (r[i]-*t1, *t2-r[i)); } ans *= 3; printf ("Case #%d:%d\n", ++kase, ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU Hotaru ' s problem (Manacher algorithm + greedy)