马拉车算法,最长回文子串——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;
}

 

原文地址:https://www.cnblogs.com/helman/p/11309805.html