题解 | #最长回文子串#

最长回文子串

https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507

#include <stdio.h>
#include <string.h>

#define MAX(a,b) (a>b)?a:b

static int Manacher(char *str)
{
    if(!str)
    {
        return -1;
    }
    int max = 0;
    int len = strlen(str);
    for(int i = 0; i < len; i++)
    {
        if(str[i] == str[i+1])    //abba
        {
            int count_abba = 0;
            for(int j = 0; j < len; j++)
            {
                if(str[i-j]==str[i+1+j] && i-j>=0 && i+j+1<len)
                {
                    count_abba += 2;
                }
                else
                {
                    break;
                }
            }
            max = MAX(max, count_abba);
        }
        else if(str[i-1] == str[i+1])    //aba
        {
            int count_aba = 1;
            for(int j = 0; j < len; j++)
            {
                if(str[i-1-j]==str[i+1+j] && i-1-j>=0 && i+j+1<len)
                {
                    count_aba += 2;
                }
                else
                {
                    break;
                }
            }
            max = MAX(max, count_aba);
        }
    }
    return max;
}
int main()
{
    char str[350] = {0};
    gets(str);
    int ret = Manacher(str);
    if(ret >= 0)
    {
        printf("%d\n", ret);
    }
    return 0;
}
全部评论

相关推荐

11-04 19:05
已编辑
东莞城市学院 单片机
不知道怎么取名字_:你这个要实习两年?哪有这么久的,感觉就是即使你毕业了,但还按实习的话,是不是不用给你缴社保公积金啥的
点赞 评论 收藏
分享
双尔:反手回一个很抱歉,经过慎重考虑,您与我的预期暂不匹配,感谢您的投递
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务