【HDOJ】4513 吉哥系列故事——完美队形II

时间:2021-07-25 12:53:51

这题目上学期就看了,不过最近发现可以用马拉车来解,而且还是基本算法。 稍微对回文串成立条件变形一下即可。

 /* 4513 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 1e5+;
int a[maxn], b[maxn*];
int Len[maxn*]; void init(int n) {
int j = ; b[j++] = -;
b[j++] = ;
rep(i, , n) {
b[j++] = a[i];
b[j++] = ;
}
} void manacher(int m) {
int n = *m+;
int p = , p0 = ;
int lid, rid; rep(i, , n+) {
if (p > i)
Len[i] = min(p-i, Len[*p0-i]);
else
Len[i] = ; while (b[i+Len[i]] == b[i-Len[i]]) {
if (b[i+Len[i]] > ) {
lid = i - Len[i];
rid = i + Len[i];
if (b[lid]>b[lid+] || b[rid]>b[rid-])
break;
}
++Len[i];
} if (i+Len[i] > p) {
p = i + Len[i];
p0 = i;
}
}
} void solve(int n) {
init(n);
manacher(n); int ans = ; n = n*+;
rep(i, , n) {
ans = max(ans, Len[i]);
}
printf("%d\n", ans-);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t;
int n; scanf("%d", &t);
while (t--) {
scanf("%d", &n);
rep(i, , n)
scanf("%d", &a[i]);
solve(n);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}