bzoj 2342-manacher板子理解O(n)

題意:給一個5e5的字符串,求最長滿足雙迴文的子串的長度。雙迴文的定義爲:一個字符串長度爲4,迴文,且前半段和後半段單獨爲迴文串。
思路:若已知條件的迴文邊界超過覆蓋了新統計點,且該點的迴文半徑可以達到上一次的點,那麼統計一次答案。
代碼:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define for1(i,n) for(int i=1;i<=n;i++)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int maxn = 5e5+5;

char ma[maxn<<1];int mp[maxn<<1],ans;

void manacher(string s,int len){
    int p = 0,r = 0,mid = 0;
    ma[p++] = '$',ma[p++] = '#';
    forn(i,len) ma[p++] = s[i],ma[p++] = '#';
    for(int i = 1;i<p;i+=2){
        mp[i] = r>i?min(mp[(mid<<1)-i],r-i):1;
        if(mp[i]>=i-mid+1) ans = max(ans,(i-mid)<<1);
        while(ma[i+mp[i]]==ma[i-mp[i]])mp[i]++;
        if(i+mp[i]>r) r = i+mp[i],mid = i;
    }
}

int main(){
    IO;
    int n;cin>>n;
    string s;cin>>s;
    int len = s.size();
    manacher(s,len);
    // forn(i,(len+1)<<1) cerr<<ma[i];
    // cerr<<'\n';
    // forn(i,(len+1)<<1) cerr<<mp[i]<<' ';
    // cerr<<'\n';
    cout << ans<<'\n';
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章