HDU-4513 (马拉车)

在原模板求p时加入条件保证从左到中心递增

#include 
#include 
using namespace std;
const int N = 1e6 + 5;
int s[N], p[N], ss[N], len, ans;
void Manacher() {
    int l = 0;
    ss[l++] = -10;
    ss[l++] = 32767;
    for (int i = 0; i < len; i++) {
        ss[l++] = s[i];
        ss[l++] = 32767;
    }
    ss[l] = 0;
    int MaxR = 0;
    int c = 0;
    for (int i = 1; i < l; i++) {
        p[i] = MaxR > i ? min(p[2 * c - i], MaxR - i) : 1;
        int cmp = ss[i];
        while (ss[i + p[i]] == ss[i - p[i]] && (cmp >= ss[i - p[i]] || ss[i - p[i]] == 32767)){
            if(ss[i - p[i]] != 32767){
        		cmp = ss[i - p[i]];            	
            }
            p[i]++;
        }
        if (i + p[i] > MaxR) {
            MaxR = i + p[i];
            c = i;
        }
        if(p[i] - 1 > ans){
        	ans = p[i] - 1;
        }
    }
}
int main()
{
	int t;
	scanf("%d", &t);
	while(t--){
		scanf("%d", &len);
		for(int i = 0; i < len; ++i){
			scanf("%d", &s[i]);
		}
		ans = 0;
		Manacher();
		printf("%d\n", ans);
	}
}

你可能感兴趣的:(Manacher)