zoukankan      html  css  js  c++  java
  • 马拉车算法,最长回文子串——HDU4513

    题目链接

    题目不仅要求是回文子串,还要求从左边到中间,升高递增

    这怎么办呢,让我们插入相邻两个身高之间的数,数值在这两个数之间?

    也许可以,但太麻烦了

    在while循环加入res[i-p[i]]<=res[i-p[i]+2]

    这样如果i-p[i]是身高值的话,满足递增

    如果是插入值的话,又是等于,满足条件

     题目代码

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<vector>
    using namespace std;
    typedef long long LL;
    const int maxn=1e5+7;
    int str[maxn],res[maxn*2],p[maxn*2];
    int t,n;
    int main(){
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            for(int i=0;i<n;i++){
                scanf("%d",&res[i*2+2]);
                res[i*2+3]=2;
            }
            res[1]=2,res[0]=1;
            int right=0,mi=0;
            int maxlen=0;
            for(int i=1;i<2*n+2;i++){
                p[i]=right>i?min(p[mi*2-i],right-i):1;
                while(res[i+p[i]]==res[i-p[i]]&&res[i-p[i]]<=res[i-p[i]+2])
                    p[i]++;
                if(i+p[i]>right){
                    right=i+p[i];
                    mi=i;
                }
                if(p[i]>maxlen){
                    maxlen=p[i];
                }
            }
            printf("%d
    ",maxlen-1);
        }
        return 0;
    }

     

  • 相关阅读:
    函数后面加const
    关于C++ const 的全面总结
    待下载的东西
    GDI与DC
    Windows GDI与DC
    认识句柄
    什么是客户区/非客户区
    OpenCV 2.4.8 +VS2010的开发环境配置
    对话框类的数据交换和检验
    怎么调处vs2010的MSDN帮助文档
  • 原文地址:https://www.cnblogs.com/helman/p/11309805.html
Copyright © 2011-2022 走看看