给定一个字串X,求它最长字串S,使得S=SR,SR为S的反序,即如果S=abc,则SR=cba
例子:X=abccba,则输出S=abccba
遍历X的子串,从最长子串开始,对每一个子串判断其是否为反身字串,是则找到所需字串
bool IsReverseString(const char *str, int s, int len)
{
const char *start = str+s, *end=str+s+len-1;
while ( start <= end)
{
if (*start== *end)
{
start++, end--;
}
else
break;
}
if (start >= end)
return true;
else
return false;
}
void PrintRS(const char *str, int s, int len)
{
const char *start = str+s, *end = str+s+len-1;
while (start != end)
printf("%c",*start++);
printf("%c", *start);
printf("\n");
}
void LongReverseString(const char *str)
{
int len = strlen(str);
int curlen = len;
while (curlen >=3)
{
int i=0;
while (i<=len-curlen)
{
if (IsReverseString(str, i, curlen))
{
PrintRS(str, i, curlen);
return;
}
i++;
}
curlen--;
}
}