这题目上学期就看了,不过最近发现可以用马拉车来解,而且还是基本算法。 稍微对回文串成立条件变形一下即可。
1 /* 4513 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorithm> 11 #include <cstdio> 12 #include <cmath> 13 #include <ctime> 14 #include <cstring> 15 #include <climits> 16 #include <cctype> 17 #include <cassert> 18 #include <functional> 19 #include <iterator> 20 #include <iomanip> 21 using namespace std; 22 //#pragma comment(linker,"/STACK:102400000,1024000") 23 24 #define sti set<int> 25 #define stpii set<pair<int, int> > 26 #define mpii map<int,int> 27 #define vi vector<int> 28 #define pii pair<int,int> 29 #define vpii vector<pair<int,int> > 30 #define rep(i, a, n) for (int i=a;i<n;++i) 31 #define per(i, a, n) for (int i=n-1;i>=a;--i) 32 #define clr clear 33 #define pb push_back 34 #define mp make_pair 35 #define fir first 36 #define sec second 37 #define all(x) (x).begin(),(x).end() 38 #define SZ(x) ((int)(x).size()) 39 #define lson l, mid, rt<<1 40 #define rson mid+1, r, rt<<1|1 41 42 const int maxn = 1e5+5; 43 int a[maxn], b[maxn*2]; 44 int Len[maxn*2]; 45 46 void init(int n) { 47 int j = 0; 48 49 b[j++] = -1; 50 b[j++] = 0; 51 rep(i, 0, n) { 52 b[j++] = a[i]; 53 b[j++] = 0; 54 } 55 } 56 57 void manacher(int m) { 58 int n = 2*m+1; 59 int p = 0, p0 = 0; 60 int lid, rid; 61 62 rep(i, 1, n+1) { 63 if (p > i) 64 Len[i] = min(p-i, Len[2*p0-i]); 65 else 66 Len[i] = 1; 67 68 while (b[i+Len[i]] == b[i-Len[i]]) { 69 if (b[i+Len[i]] > 0) { 70 lid = i - Len[i]; 71 rid = i + Len[i]; 72 if (b[lid]>b[lid+2] || b[rid]>b[rid-2]) 73 break; 74 } 75 ++Len[i]; 76 } 77 78 if (i+Len[i] > p) { 79 p = i + Len[i]; 80 p0 = i; 81 } 82 } 83 } 84 85 void solve(int n) { 86 init(n); 87 manacher(n); 88 89 int ans = 0; 90 91 n = n*2+1; 92 rep(i, 1, n) { 93 ans = max(ans, Len[i]); 94 } 95 printf("%d ", ans-1); 96 } 97 98 int main() { 99 ios::sync_with_stdio(false); 100 #ifndef ONLINE_JUDGE 101 freopen("data.in", "r", stdin); 102 freopen("data.out", "w", stdout); 103 #endif 104 105 int t; 106 int n; 107 108 scanf("%d", &t); 109 while (t--) { 110 scanf("%d", &n); 111 rep(i, 0, n) 112 scanf("%d", &a[i]); 113 solve(n); 114 } 115 116 #ifndef ONLINE_JUDGE 117 printf("time = %d. ", (int)clock()); 118 #endif 119 120 return 0; 121 }