HDU - 4513 吉哥系列故事――完美队形II(manacher)

题目大意:中文题了

解题思路:manacher的裸题,只要再加相等和大小判断就可以

#include 
#include 
#include 
using namespace std;
const int N = 200010;
const int INF = 0x3f3f3f3f;

int num[N], p[N];
int n, len;

void init () {
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", &num[i]);
    for (int i = n - 1; i >= 0; i--) {
        num[i * 2 + 2] = num[i];
        num[i * 2 + 1] = INF;
    }
    num[2 * n + 1] = INF;
    num[0] = num[2 * n + 2] = -1;
}

void solve() {
    int ans = 0, mx = 0, id;
    for (int i = 2; i < 2 * n + 1; i++) {
        if (mx > i) p[i] = min(mx - i, p[2 * id - i]);
        else p[i] = 1;
        int m = num[i];

        if (num[i] == INF) m = num[i + 1] > num[i - 1] ? num[i + 1]: num[i - 1];
        for (; num[i + p[i]] == num[i - p[i]]; p[i]++) {
            if (num[i + p[i]] == -1 || num[i - p[i]] == -1) break;
            if (num[i + p[i]] == INF) continue;
            if (m >= num[i + p[i]]) 
                m = num[i + p[i]];
            else break;
        }
        if (ans < p[i]) ans = p[i];
        if (i + p[i] >= mx) {
            id = i;
            mx = i + p[i];
        }
    }
    printf("%d\n", ans - 1);
}

int main() {
    int test;
    scanf("%d", &test);
    while (test--) {
        init();
        solve();
    }
    return 0;
}

你可能感兴趣的:(ACM-数据结构-KMP)