http://acm.hdu.edu.cn/showproblem.php?pid=5371
/* 先用Manacher算法得出最长回文子串,然后用set维护ans的值 对所有回文的长度进行排序, 那么之后的点如果覆盖了最接近的点那么那么点肯定是覆盖了当前点,用二分得到最近不大于u的距离 S.upper_bound(id+u) 从起始到末尾得到第一个大于id+u的迭代器, S.lower_bound(id-u) 从起始到末尾得到第一个不小于id-u的迭代器 set 容器自动从小到大排序 */ /************************************************ * Author :Powatr * Created Time :2015-8-12 19:49:44 * File Name :1003.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAXN = 4e5 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int n; int p[MAXN]; int len; int a[MAXN]; int aa[MAXN]; set<int> S; set<int>::iterator it; struct edge{ int u, id; }b[MAXN]; bool cmp(edge i, edge j) { return i.u > j.u; } void inti() { memset(p, 0, sizeof(p)); memset(aa, -1, sizeof(aa)); for(int i = 1; i <= n; i++) aa[i*2] = a[i]; len = n*2 + 1; } int read() { char z = getchar(); while(z < ‘0‘ || z > ‘9‘) z = getchar(); int ans = 0; while( z >= ‘0‘ && z <= ‘9‘){ ans = ans*10 + z - ‘0‘; z = getchar(); } return ans; } void Manacher() { int mx = 0; int id; for(int i = 1; i <= len; i++){ if(mx > i){ p[i] = min(p[2*id-i], mx - i); } else p[i] = 1; for( ;aa[i+p[i]] == aa[i-p[i]]; p[i]++); if(p[i] + i > mx){ mx = p[i] + i; id = i; } } } int main(){ int T; scanf("%d", &T); for(int cas = 1; cas <= T; cas++){ S.clear(); n = read(); for(int i = 1; i <= n; i++) a[i] = read(); inti(); Manacher(); for(int i = 1; i <= n; i++){ b[i].u = (p[i*2+1]-1)/2; b[i].id = i; } sort(b+1, b+n+1, cmp); int ans = 0; for(int i = 1; i <= n; i++){ int id = b[i].id; int u = b[i].u; if(u == 0) break; S.insert(id); it = S.upper_bound(id+u); it--; ans = max(ans, *it - id); it = S.lower_bound(id-u); ans = max(ans, id - *it); } printf("Case #%d: ", cas); printf("%d\n", 3*ans); } return 0; }
HDU5371——Manacher+set维护——Hotaru's problem
时间: 2024-10-13 01:25:37