manacher算法

模板,无特殊说明orz

#include <bits/stdc++.h>
using namespace std;
#define limit (31500000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
#define debug(x) cout<<x<<endl
typedef long long ll;
typedef unsigned long long ull;
inline ll read(){
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}
    return x * sign;
}//快读
void write(ll x){
    if(x < 0) putchar('-'),x = -x;
    if(x / 10) write(x / 10);
    putchar(x % 10 + '0');
}
int kase;
int n,m,k;
int a[limit];
int b[limit];
char str[limit],s[limit];
int main() {
#ifdef LOCAL
    FOPEN;
#endif
    cin>>s+1;
    str[1] = str[0] = '#';
     n = strlen(s + 1);
    rep(i,1,n){
        str[i<<1] = s[i];
        str[i * 2 + 1] = '#';
    }
    n = n * 2 + 2;
    int ans = 0;
    int mid = 1 , maxr = 1;
    rep(i,2,n-1){
        if(i < maxr)a[i] = min(1ll * a[(mid<<1) - i],a[mid] + mid - i);
        else a[mid]= 1;
        while (str[i - a[i]] == str[i + a[i]])++a[i];
        if(i + a[i] > maxr){
            maxr = a[i] + i;
            mid = i;
        }
        ans = max(a[i],ans);
    }
    write(ans - 1);
    return 0;
}

 

posted @ 2020-12-15 12:29  tiany7  阅读(80)  评论(0编辑  收藏  举报