BZOJ 2342([Shoi2011]双倍回文-manacher+set)

BZOJ 2342([Shoi2011]双倍回文-manacher+set)_第1张图片
N<=500000

显然 wwRwwR 本身就是偶数回文串
设它为 (x2l+1,xl),(xl+1,x),(x+1,y),(x+l+1,x+2l)
枚举这个回文串的中点 x ,找最大的y使 ypyxxyx+px/2

第一个条件可以用插入set时的顺序维护,第二个lower_bound即可

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second 
#define MAXN (1000000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}

char s[MAXN];
int p[MAXN],n;
void manacher() {
    p[0]= s[0] == s[1];

    int len=p[0] ,last=0;

    For(i,n-1) {
        p[i]=0;
        if (last+len>=i) p[i]=min(p[2*last-i],last+len-i);
        while (s[i+p[i]+1]==s[i-p[i]] && p[i]<=i) p[i]++;

        if (last+len<i+p[i]) {
            last=i,len=p[i];
        }       

    }
}

set< int > S;
set<int>::iterator it; 
pair<int,int> pa[MAXN]; 

int main()
{
// freopen("bzoj2342.in","r",stdin);

    cin>>n>>s;
    manacher();

    Rep(i,n) pa[i]=mp(i-p[i],i);
    sort(pa,pa+n);
    int ptr=0;

    int ans=0;
    Rep(i,n) {
        while (ptr<n && pa[ptr].fi<=i) S.insert(pa[ptr++].se);

        it = S.upper_bound(i+p[i]/2);
        if (it == S.begin() ) continue;
        it--;
        ans = max ( ans , *it - i ); 
    } 
    cout<<4*ans<<endl;


    return 0;
}

你可能感兴趣的:(BZOJ 2342([Shoi2011]双倍回文-manacher+set))