题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
#include <stdio.h> #include <string.h> int judgestr(char* str, int i, int j) { char str1[360]; for (int a = i, b = j; a <= j; a++, b--) { str1[b] = str[a]; } for ( int k = i; k <= j; k++) { if (str1[k] != str[k]) return 0; } return 1; } int main() { char str[360]; gets(str); int x1 = -1, x2 = -1, len = -1; for (int i = 0; i < strlen(str); i++) { for (int j = i; j < strlen(str); j++) { if (judgestr(str, i, j) == 1 && len < j - i + 1) { x1 = i; x2 = j; len = j - i + 1; } } } printf("%d\n", len); /* for (int k = x1; k <=x2; k++) { printf("%c",str[k]); }*/ return 0; }