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.
我:应该是最简单最无脑的做法了。
class Solution {
public:
string longestPalindrome(string s) {
int len=s.size();
int m,n,begin,end,l,ll=0;
string r;
if(len<=1)
return s;
for(int i=0;i<len-2;i++)
{
if(s[i]==s[i+1])
{
m=i;n=i+1;
while(m>=0&&n<len)
{
if(s[m]==s[n])
{
m--;
n++;
}
else
break;
}
m++;n--;
l=n-m+1;
if(l>ll)
{
begin=m;
end=n;
ll=l;
}
}
if(s[i]==s[i+2])
{
m=i;n=i+2;
while(m>=0&&n<len)
{
if(s[m]==s[n])
{
m--;
n++;
}
else
break;
}
m++;n--;
l=n-m+1;
if(l>ll)
{
begin=m;
end=n;
ll=l;
}
}
}
if(ll==0)
{
begin=len-2;
end=len-1;
}
for(int i=begin;i<=end;i++)
{
r.push_back(s[i]);
}
return r;
}
};