首页 > 代码库 > hdu 4513 最长回文子串

hdu 4513 最长回文子串


就是在最基础的回文子串中多加个判断条件就行


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

int num1[100010],num2[200010];
int min(int a,int b)
{
    return a<b?a:b;
}
int main()
{
    int T,n,i,j;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        j=1;
        num2[0]=-1;
        num2[1]=1;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num1[i]);
            num2[++j]=num1[i];
            num2[++j]=1;
        }
        int mark[200010];
        memset(mark,0,sizeof(mark));
        int right=0,k=0,id;
        for(i=0;i<j;i++)
        {
            if(right<=i) mark[i]=1;
            else
            mark[i]=min(mark[2*id-i],right-i);
            while(num2[i+mark[i]]==num2[i-mark[i]])
            {
                if(num2[i+mark[i]]==1) mark[i]++;
                else if(num2[i+mark[i]]<=num2[i+mark[i]-2]&&num2[i-mark[i]]<=num2[i-mark[i]+2]) mark[i]++;
                else break;
            }
            if(mark[i]+i>right)
            {
                right=mark[i]+i;
                id=i;
            }
            if(mark[i]>k) k=mark[i];
        }
        printf("%d\n",k-1);
    }
    return 0;
}

hdu 4513 最长回文子串