题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
#include <stdio.h> #include <string.h> int main() { char str[1000]; scanf("%s",str); int len = strlen(str); int i; int max = 0; for(i=0;i<len;i++) { int a = 0; if(str[i] == str[i+1]) //abba型 { while(str[i-a] == str[i+1+a]) { a++; } if(max < a) max = 2*a; if(a > i+1 && i+1+a > len) max = 2*a -2; //对于整个字符串都是回文子串 } else if(str[i-1] == str[i+1]) //abcba型 { while(str[i-1-a] == str[i+1+a]) { a++; } if(max < a ) max = 2*a + 1; if(a > i+1 && i+1+a > len) max = 2*a -1; //对于整个字符串都是回文子串 } } printf("%d",max); return 0; }