标签:
吉哥系列故事――完美队形II
Description
Input
Output
Sample Input
Sample Output
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <set> using namespace std; const int maxn = 1e5 + 7; int n; int s[maxn]; int ss[maxn<<1]; int p[maxn<<1]; void make() { ss[0] = -1; int pos = 0; for(int i = 0; i < n; i++) { ss[++pos] = 0; ss[++pos] = s[i]; }ss[++pos] = 0; n = n * 2 + 2; //for(int i = 0; i < n; i++) printf("%d ", ss[i]); puts(""); } void solve() { int maxx = 0, id; for(int i = 1; i < n; i++) { if(maxx > i) p[i] = min(p[id * 2 - i], p[id] + id - i); else p[i] = 1; int Min; if(ss[i] > 0) Min = ss[i]; else Min = ss[i + 1]; while(ss[i - p[i]] == ss[i + p[i]] && ss[i - p[i]] <= Min) { //printf("are you here?\n"); if(ss[i - p[i]] > 0) Min = ss[i - p[i]]; if(i + p[i] > maxx) { maxx = i + p[i]; id = i; } p[i]++; } } int ans = 0; for(int i = 1; i < n; i++) ans = max(ans, p[i]); printf("%d\n", ans - 1); } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &s[i]); } make(); solve(); } }
HDU 4513 吉哥系列故事――完美队形II (manacher算法)
标签:
原文地址:http://www.cnblogs.com/lonewanderer/p/5648075.html