zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    js-js系列-数据类型-概念
    js-基础总结3
    js-基础总结2
    js-基础总结1
    js-面试题
    webpack-模块化
    js-对象常用方法
    js-事件冒泡-事件捕获-事件委托
    js-call aplly bind2
    aioxs实现token无感刷新
  • 原文地址:https://www.cnblogs.com/war1111/p/12449378.html
Copyright © 2011-2022 走看看