#include <iostream> #include<string> using namespace std; string findLongestPalindrome(string &s) { int length=s.size(); int maxlength=0; int start,j,k; for(int i=0;i<length;i++) { if(length%2!=0){//奇偶判断 j=i-1;k=i+1; }else{ j=i;k=i+1; } while(j>=0&&k<length&&s.at(j)==s.at(k)) { if(k-j+1>maxlength) { maxlength=k-j+1; start=j; } j--; k++; } } if(maxlength>0) return s.substr(start,maxlength); return NULL; } void main() { string s; getline(cin,s); cout<<findLongestPalindrome(s)<<endl; }
更多信息:
http://blog.csdn.net/kangroger/article/details/37742639
时间: 2024-10-28 10:43:57