int longestReverse(char *str)
{
int count = 0;
int max = 0;
int length = strlen(str);
stack<char> s;
s.push(*str);
for(int i=1; i<length; i++)
{
if(s.size() > 0 && str[i] == s.top())
{
count+=2;
s.pop();
}
else
{
if(count > max)
{
max = count;
}
count = 0;
s.push(str[i]);
}
}
//注意需要添加这最后一步,防止在字符串尾部出现的最长回文不会在for循环中改写max
if(count > max)
{
max = count;
}
return max;
}
字符串的最大回文长度
最新推荐文章于 2023-01-03 22:19:37 发布