题目给出的整数哦, 不过也完全可以通过manacher算法来实现查找最长的回文哦.
这个道题比平常的回文长度的题目加多一个判断而已啦.
#include <stdio.h>
#include <string.h>
#define MAX 100005
#define min(a, b) (a > b ? b : a)
int q[MAX], newQ[MAX*2];
int p[MAX*2];
int main() {
int T, n, m;
int mx, maxLen, id;
int i, z;
int a, b;
while (scanf("%d", &T) != EOF) {
while (T--) {
memset(newQ, 0, sizeof(newQ));
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &q[i]);
}
m = 2*n;
newQ[0] = -1, newQ[1] = 0;
for (i = 1; i <= n; i++) {
newQ[i*2] = q[i];
newQ[i*2 + 1] = 0;
}
mx = maxLen = id = 0;
for (i = 1; i <= m; i++) {
if (mx > i) {
p[i] = min(p[id*2 - i], mx - i);
} else {
p[i] = 1;
}
while (newQ[i - p[i]] == newQ[i + p[i]]) {
if (newQ[i + p[i]] == 0) {
p[i]++;
} else {
a = i - p[i];
b = i + p[i];
if (newQ[a] <= newQ[a + 2] && newQ[b - 2] >= newQ[b]) {
p[i]++;
continue ;
}
break ;
}
}
if (p[i] + i > mx) {
mx = p[i] + i;
id = i;
}
if (maxLen < p[i]) {
maxLen = p[i];
}
}
printf("%d\n", maxLen - 1);
}
}
return 0;
}