http://47.95.147.191/problem/R2D2-A

http://47.95.147.191/problem/R2D2-A
manacher模板题,当时没写出来真的是

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 41000000
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)

using namespace std;
char a[N],s[N],b[N];
int ans,n;
void in(int &x){
    int y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

void manacher(){
    n=n*2+1;
    vector<int> d1(n);
    for (int i=1,l=1,r=-1;i<=n;i++) {
        int k=(i>r)?1:min(d1[l+r-i],r-i);
        while (1<=i-k&&i+k<=n&&s[i-k]==s[i+k]) k++;
          d1[i]=k--;
          if(i+k>r){
          l=i-k;
          r=i+k;
        }
        if(d1[i]==i)
        ans=max(ans,2*d1[i]-1);
    }
}

signed main(){
    in(n);
    cin>>(b+1);
    For(i,1,n) a[i]=b[n-i+1];
    For(i,1,2*n+1){
        if(i&1) s[i]='#';
        else s[i]=a[i>>1];
    }
    manacher();
    ans/=2;
    n--;n/=2;
    o(n-ans);
    return 0;
}

 

posted @ 2020-03-09 16:20  WeiAR  阅读(135)  评论(0编辑  收藏  举报