题意:中文题面。
思路:题目要求队列最长,要对称,还要连续,这不就是最长回文串嘛!还需要从左到中间非递减,只需要在manacher推进的时候多一个条件就行了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int maxn = 100005;
int s1[maxn], s2[maxn * 2], p[maxn * 2];
int n;
int manacher(){
s2[0] = -1;
for(int i = 0; i <= n; ++i){//构造串,首尾分别放-1、-2,中间补0
s2[i * 2 + 1] = 0;
s2[i * 2 + 2] = s1[i];
}
s2[n * 2 + 2] = -2;
int len = n * 2 + 1;
int pos = 0, maxlen = 1;
p[1] = 1;
for(int i = 2; i < len; ++i){
if(i < pos + p[pos]){
p[i] = min(p[2 * pos - i], pos + p[pos] - i);
} else {
p[i] = 1;
}
while(((i - p[i]) & 1) || (s2[i - p[i]] == s2[i + p[i]] && s2[i - p[i]] <= s2[i - p[i] + 2])){
//多加一个((i - p[i]) & 1)避免误伤自己添加的0
++p[i];
}
if(pos + p[pos] < i + p[i]){
pos = i;
}
if(p[i] > maxlen){
maxlen = p[i];
}
}
return maxlen - 1;
}
int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &s1[i]);
}
printf("%d\n", manacher());
}
return 0;
}