Enter a string to find the longest palindrome substring in the list. The meaning of a substring is: a string fragment that appears consecutively in the original string. The meaning of a palindrome is: look and look backwards. such as ABBA and YYXYY. When judging, all punctuation marks and spaces should be ignored, and the case should be ignored, but the output should remain the same (do not output extra characters at the header and tail of the palindrome string). The input string is not longer than 5000 and occupies a separate line. Should output the longest palindrome string, if there are multiple, the output starting position is the most left.
Sample input: Confuciuss say:madam,i ' m Adam.
Sample output: Madam,i ' m Adam
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAXN 5000 + 10char BUF[MAXN], s[maxn];//intint P[MAXN]; int main (int argc, char *argv[]) {int n, m = 0, I, j, x, y;//m = 0 fgets (buf, sizeof (s), stdin); int max = 0;//max = 0; n = strlen (BUF); for (i = 0; i < n; i++) {if (Isalpha (Buf[i])) {S[m] = ToUpper (Buf[i]); p[m++] = i; }} for (i = 0; i < m; i++) {for (j = 0; i-j>=0 && i+j<m; j + +)//i+j+1 {if (S[i-j]! = S[i+j]) break; if (2*j+1 > Max) {max = 2*j+1; x = p[i-j]; y = p[i+j];} //; } for (j = 0; i-j>=0 && i+j+1<m; j + +)//2*j+2 {if (s[i-j]! = s[i+j+1]) break; if (2*j+2 > Max) {max = 2*j+2; x = p[i-j]; y = p[i+j+1];} }} for (i = x; i <= y; i++) {printf ("%c", Buf[i]),//%c} printf ("\ n");//Last to add line-break system ("PAUSE"); return 0;}
Summary: 1 pairs of string preprocessing
2 ctype.h or C-' a ' + ' a '
32 cases ABC or ABBC
4 Fgets up to MAXN-1 characters, the second is ' \ n ' and the last one is ' + '
5 do not recommend the use of gets, there is a buffer overflow vulnerability
Algorithm Competition Introduction Classic example 3-4 palindrome string