最大回文子串

原文链接:https://blog.csdn.net/qq_31820761/article/details/90635480

#include <iostream>
#include <string>
using namespace std;
string LongestPalindrome(string s)
{
	int len = s.size();
	if (len==0 || len==1)
	{
		return s;
	}
	int start = 0;//记录回文子串起始位置
	int end = 0;//记录回文子串终止位置
	int mlen = 0;//记录最大回文子串的长度
	for (int i=0;i < len;i ++)
	{
		int len1 = ExpendAroundCenter(s,i,i);
		int len2 = ExpendAroundCenter(s,i,i+1);
		mlen = max(max(len1,len2),mlen);
		if (mlen>end-start+1)
		{
			start = i-(mlen-1)/2;
			end = i+mlen/2;
		}
	}
	return s.substr(start,mlen);
}
int ExpendAroundCenter(string s,int left,int right)
{
	int L= left;
	int R= right;
	while (L>=0 && R < s.length() && s[R] == s[L])
	{
		L--;
		R++;
	}
	return R - L -1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值