题解 | #最长回文子串#

最长回文子串

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

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

int main() {
    char str[350] = {0};
    //scanf("%s", str);
    fgets(str, 352, stdin);
    //printf("%s",str);
    int len = strlen(str);
    int x[350];

    for (int i = 0; i < len - 2; i++) {
        if (str[i] == str[i + 1]) {
            x[i] = 2;
            for (int j = 1; j < len / 2; j++) {
                if (str[i - j] == str[i + 1 + j]) {
                    x[i] += 2;
                    //  printf("%c",str[i+j+1]);
                } else {            //注意一旦找不到要切出循环否则函数不可控
                    break;
                }
            }
        } else {
            x[i] = 1;
            for (int j = 1; j < len / 2; j++) {
                if (str[i - j] == str[i + j]) {
                    x[i] += 2;
                } else {
                    break;
                }
            }

        }
        // printf("%d  %d\n",x[i],i);

    }


    int max = x[0];
    for (int i = 1; i < len - 2; i++) {
        if (x[i] > max) {
            max = x[i];
        }
    }

    printf("%d", max);


    return 0;
}

全部评论

相关推荐

野猪不是猪🐗:我assume that你must技术aspect是solid的,temperament也挺good的,however面试不太serious,generally会feel style上不够sharp
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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