/* 最长回文子串。 */ #include<stdio.h> #include<malloc.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<string.h> #include <iostream> int getMax(int a,int b){ if(a<b) return b; return a; } int getLen(char *s,int i,int j){ int len=strlen(s); while(i<len&&j>=0&&s[i]==s[j]){ i--; j++; } return j-i-1; } char * longestPalindrome(char * s){ int curlen=0,maxlen=0,i,j,len=strlen(s),start; char *rs; for(i=0;i<len;i++){ curlen=getMax(getLen(s,i,i),getLen(s,i,i+1)); if(curlen>maxlen){ maxlen=curlen; start=i-(maxlen-1)/2; } } rs=(char*)malloc(sizeof(char)*100); strncpy(rs,s+start,maxlen); return rs; } int main() { char s[100],*rs; gets(s); rs=longestPalindrome(s); printf("%s ",rs); return 0; }