Algorithm Competition Introduction Classic example 3-4 palindrome string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.