最长回文串 O(n)复杂度(manacher算法)

manacher算法:点击打开链接

邝斌模板:

void Manacher(int len){
    int l = 0;
    s[l++] = 1;
    s[l++] = 2;
    for(int i = 0;i < len;i ++){
        s[l++] = line[i];
        s[l++] = 2;
    }

    int id = 0, mx = 0;
    for(int i = 1;i < l;i ++){
        Mp[i] = mx > i ? min(Mp[2*id-i],mx-i) : 1;
        while(s[i + Mp[i]] == s[i - Mp[i]])   Mp[i]++;
        if(i + Mp[i] > mx){
            mx = i+Mp[i];
            id = i;
        }
    }
}


你可能感兴趣的:(最长回文串 O(n)复杂度(manacher算法))