求对称子字符串的最大长度,如abcbae,那么最大长度为5。
动态规划,O(n*n)
bool a[100][100] = {false};
int get_longest(char *b)
{
int len = 0;
int slen = strlen(b);
int first = 0;
int last = 0;
for (last = 0; last < slen; ++last)
{
for (first = 0; first <= last; ++first)
{
if (first+1 <= last-1)
a[last][first] = (a[last-1][first+1] && b[first]==b[last]);
else
a[last][first] = (b[first]==b[last]);
if (a[last][first] && (last - first +1 > len))
len = last - first +1 ;
}
}
return len;
}