题解 | #最长回文子串#
最长回文子串
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; }