Question: Given a string S, find the longest palindromic substring in S. you may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. search for and output the longest return string in the string. I didn't expect any very good algorithms, so I just put some pruning on it. Enumerate the length of the input string, starting from the longest, to find whether the input string of the current length exists. If it is found, a loop pops up. If it is not found, the length of the return string is reduced. L record the subscript of the original string that meets the conditions, and r record the length of the string.
class Solution { public: string longestPalindrome(string s) { int i,j,k,len=s.length(),l=0,r=1; for(k=len;k>=2;--k) { for(i=0;i+k<=len;++i) { for(j=0;j<k/2;++j) { if(s[i+j]!=s[i+k-j-1])break; } if(j!=k/2)continue; else { l=i;r=k; } } if(r==k)break; } string temp; return temp.assign(s,l,r); } };