最长回文子串(Longest Palindromic Substring )

回文字符串:abcba,这就是一个回文字符串。最长回文字符串的顾名思义就是一个字符串的所有子串中最长的回文串。

例如:cabcba最长的回文子串就是abcba;

有两种解决方法:1.动态规划,2.中间扩展法

1、动态规划法

一般求字符串子串之类的问题都会涉及到回溯,暴力解法效率太低,这时都要想到动态规划法。

动态规划的核心就是:规划表的递推公式和初始条件。

此题的规划表dp[i][j]表示表示字符串s中下标从 i 到 j 的字符组成的子串是否是回文串。

递推公式: dp[ i ][ j ] = dp[ i + 1][ j - 1] && s[ i ] == s[ j ]

初始条件:i=j和j=i-1,也就是长度为1和2,它们不能由子问题得到。

这里可以认为j>i(对称)。

代码如下:

string longestPalindrome(string s) {
<span style="white-space:pre">	</span>if(s.size() == 0)
<span style="white-space:pre">		</span>return "";
<span style="white-space:pre">	</span>int max=1;
<span style="white-space:pre">	</span>int n = s.size();
<span style="white-space:pre">	</span>bool **table = new bool*[n];
<span style="white-space:pre">	</span>for(int i=0; i<n; i++){
<span style="white-space:pre">		</span>table[i] = new bool[n];
<span style="white-space:pre">	</span>}

<span style="white-space:pre">	</span>//初始条件
<span style="white-space:pre">	</span>int startPos=0;
<span style="white-space:pre">	</span>for(int i=0; i<n; i++){
<span style="white-space:pre">		</span>table[i][i] = true;
<span style="white-space:pre">		</span>if(i+1<n){
<span style="white-space:pre">			</span>if(s[i] == s[i+1]){
<span style="white-space:pre">				</span>table[i][i+1] = true;
<span style="white-space:pre">				</span>max=2; 
<span style="white-space:pre">				</span>startPos = i;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>else
<span style="white-space:pre">				</span>table[i][i+1] = false;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}

<span style="white-space:pre">	</span>//动态规划求解
<span style="white-space:pre">	</span>for(int i=0; i<n; i++){
<span style="white-space:pre">		</span>for(int j=i+2; j<n; j++){
<span style="white-space:pre">			</span>if(table[i+1][j-1] && s[i] == s[j]){
<span style="white-space:pre">				</span>table[i][j] = true;
<span style="white-space:pre">				</span>int curLen = j-i+1;
<span style="white-space:pre">				</span>if(curLen>max){
<span style="white-space:pre">					</span>max = curLen;
<span style="white-space:pre">					</span>startPos = i;
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>else
<span style="white-space:pre">			</span>table[i][j] = false;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}

<span style="white-space:pre">	</span>//释放内存空间
<span style="white-space:pre">	</span>for(int i=0; i<n; i++)
<span style="white-space:pre">		</span>delete[] table[i];
<span style="white-space:pre">	</span>delete[] table;
<span style="white-space:pre">	</span>return s.substr(startPos,max);
}

2、 中间扩展法

动态规划需要额外的O(n2),考虑到回文字符串的特点,回文串以中心轴对称,所以如果我们从下标 i 出发,用2个指针left和right向 i 的两边扩展判断是否相等,那么只需要对0到n-1的下标都做此操作,就可以求出最长的回文子串。但需要注意的是,回文字符串有奇偶对称之分,即"abcba"与"abba"2种类型,因此需要在代码编写时都做判断。

string longestPalindrome(string s) {
	if(s.size() == 0)
		return "";
	int n = s.size();
	int max=1;
	int startPos = 0;
	for (int i=0; i<n; i++)
	{
		int oddLen=0,evenLen=0,curLen;
		//奇对称长度
		oddLen = Palindromic(s,i,i);
		//偶对称长度
		if(i+1<n)
			evenLen = Palindromic(s,i,i+1);
		curLen = oddLen>evenLen ? oddLen : evenLen;
		if(curLen > max){
			max = curLen;
			if(max & 0x01)
				startPos = i-max/2;
			else
				startPos = i-(max-1)/2;
		}
	}
	
	return s.substr(startPos,max);
}

//向左右两边遍历,获取回文串的长度
int Palindromic(const string &str, int left, int right){
	int len = 0;
	int n = str.size();
	while(left>=0 && right<=n && str[left]==str[right]){
		left--;
		right++;
	}
	len = right-left-1;
	return len;
}  





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值