最长回文子串
/* 最长回文子串。 */ #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\n",rs); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步