• 最长回文子串


    len <1000000;

       F[i]= min(F[2*j-i],F[j]-2*(i-j)); j为小于i =  > j+F[j]/2 最大的;

    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <cstdio>
    #include <vector>
    using namespace std;
    const int maxn = 1000005*2;
    char s[maxn];
    char str[maxn];
    int ans;
    int F[maxn];
    void solve(int n)
    {
           F[0]=1;
           int ma =0;
           for(int i=1; i<n; i++)
            {
                int d=ma*2 -i;
                int los=0;
                if(d<0)
                    {
                        los=0;
                    }
                else los=F[d];
    
                if( d>=0&&F[ma]-(i-ma)*2<0 )
                    {
                        los=0;
                    }
                else if(d>=0)
                    los=min(los,F[ma]-(i-ma)*2);
    
                F[i]=los;
    
                if(i+F[i]/2> ma+F[ma]/2)
                    ma=i;
                int L = i-los/2,R=los/2+i;
    
                while(L-1>=0&&R+1<n&&str[L-1]==str[R+1])
                    {
                         L--;R++;
                    }
    
                d=R-L+1;
                F[i]=d;
                if(i+F[i]/2> ma+F[ma]/2)ma=i;
                if(str[i]=='#')
                    {
                        d--;
                        if(d>0&&str[L]!='#')
                        {
                            ans=max( 2+max( (d-2)/2,0),ans);
                        }
                        else if(d>0)
                        {
                            ans=max(d/2,ans  );
                        }
                    }
                else
                    {
                        if(d>2&&str[L]!='#')
                            {
                                ans=max(ans, 1+(d-1)/2) ;
                            }
                        else if(d>2)
                            {
                                ans=max(ans, 1+(d-3)/2);
                            }
                    }
            }
    }
    int main()
    {
         int cas;
         scanf("%d",&cas);
         for( int cc =1; cc <=cas; cc++)
            {
               scanf("%s",s);
               int n =strlen(s);
               int loc=0;
               for(int i=0; i<n; i++)
                {
                    str[loc++]=s[i];
                    str[loc++]='#';
                }
               ans =1;
               solve(loc-1);
               printf("%d
    ",ans);
            }
    
        return 0;
    }
    View Code
  • 相关阅读:
    spring注解-事务
    docker 安装
    docker 简单介绍
    jupyter配置
    docker 桌面镜像内安装gui程序启动报错
    Linux下安装matlab
    拉取cmake镜像并测试
    桌面镜像安装gui程序
    docker + pycharm 运行
    docker + vscode 运行
  • 原文地址:https://www.cnblogs.com/Opaser/p/4491394.html
Copyright © 2020-2023  润新知