题解 | #最长回文子串#

最长回文子串

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

#include <stdio.h>
#include <string.h>
#define LEN 350
int main() {
    char s[LEN];
    while (scanf("%s", s) != EOF) {
        int len = strlen(s);
        int maxlen = 0;
        int tempi, tempj, tempmax;
        for(int i = 0; i < len; i++) {
            tempmax = 0;
            tempi = i;
            tempj = i + 1;
            while((tempi >= 0) && (tempj < len) && (s[tempi--] == s[tempj++])) {
                tempmax++;
            }
            if(tempmax * 2 > maxlen) maxlen = tempmax * 2;
            tempmax = 0;
            tempi = i;
            tempj = i + 2;
            while((tempi >= 0) && (tempj < len) && (s[tempi--] == s[tempj++])) {
                tempmax++;
            }
            if(tempmax * 2 + 1 > maxlen) maxlen = tempmax * 2 + 1;
        }
        printf("%d\n", maxlen);
    }
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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