搜寻最长回文子串的解法之比较

问题描述:

回文字符串,也即正反两个方向读取的字符串应该一样。比如ABCDCBA。

        本文的问题是,给定一个字符串输出其最长的回文串。


解决方法:

1.暴力搜索(Brute force)

        2.动态规划(dynamic programming)

               假定:inputStr 为输入串

               table[head][tail]:表示头方向从head开始,尾方向从tail开始,是否存在回文串,

               若存在,table[head][tail] = 1,否则table[head][tail] = 0;

     状态转移方程:table[head][tail] = 1,当table[head+1][tail=-1] = 1 且 inputStr[head] == inputStr[tail];

 

源代码:

#ifndef _DAYNAMIC_PROGRAMMINGSET_H_
#define _DAYNAMIC_PROGRAMMINGSET_H_

#include <string>
#include <stdlib.h>
#include <stdio.h>


/*
*
*
*/
template<class T>
T  MaxValue( const T& first, const T& second )
{
	if( first > second )
	{
		return first;
	}

	return second;
}


/*
* The implementation of the method of brute force
*
*/
int LongPalindromicSubstrBrute( const char* str, std::string& palinDromicStr )
{
	int maxVal = -1;
	size_t len = strlen( str );
	int startIdx = -1;

	for( int i = 0; i < len; i++ )
	{
		int start = i;
		int maxLenStr = 0;
		for( int j =  len - 1; j >= start; j-- )
		{
			if( str[start] == str[j] )
			{
				start++;
				maxLenStr += 2;
			}
			else
			{
				maxLenStr = 0;
				start = i;
			}
		}

		if( maxLenStr > maxVal  )
		{
			startIdx = i;
			maxVal = maxLenStr;
		}
	}

	std::string strObj(str);
	palinDromicStr = strObj.substr( startIdx, maxVal );

	return maxVal;
}

/*
* helper function
*
*/
void ConstructTable( size_t tableLen, int**& table )
{
	table = new int*[tableLen];
	assert( table );

	for( int i = 0; i < tableLen; i++ )
	{
		table[i] = new int[ tableLen ];
		memset( table[i], 0x00, sizeof(int)*(tableLen ));
		assert( table[i] );
	}
}


/*
* helper function
*
*/
void DestructTable( size_t tableLen, int** table )
{
	for( int i = 0; i < tableLen; i++ )
	{
		delete [] table[i];
	}

	delete [] table;
}

/*
* the implementation of dynamic programming
*
*/
int LongPalindromicSubstrDPExt( const char* str, std::string& palindromicStr )
{
	size_t strLen = strlen( str );
	int** table = 0;
	ConstructTable( strLen + 1, table );

	for( int i = 0; i <= strLen; i++ )
		table[i][i] = 1;

	int curidx = 0;
	int maxLen = -1;
	for( int i = 0; i < strLen - 1; i++ )
	{
		if( str[i] == str[i + 1] )
		{
			table[i][i + 1] = 1;
			maxLen = 2;
			curidx = i;
		}
	}


	for( int i = 3; i <= strLen; i++ )
	{
		//head pos index
		for( int head = 0; head < strLen - i + 1; head++ )
		{
			//tail pos index
			int tail = head + i - 1;
			if( table[head + 1][tail - 1] && str[head] == str[tail] )
			{
				table[head][tail] = 1;
				if( i > maxLen )
				{
					maxLen = i;
					curidx = head;
				}
			}

		}
	}

	std::string strObj(str);
	palindromicStr = strObj.substr( curidx, maxLen );

	DestructTable( strLen + 1, table );

	return maxLen;

}

/*
* the implementation of dynamic programming
*
*/
int LongPalindromicSubstrDP( const char* str, std::string& palindromicStr )
{
	size_t strLen = strlen( str );
	int** table = 0;
	ConstructTable( strLen + 1, table );

	for( int i = 0; i <= strLen; i++ )
		table[i][i] = 1;

	for( int i = 0; i < strLen - 1; i++ )
	{
		if( str[i] == str[i + 1] )
			table[i][i + 1] = 1;
	}

	int maxLen = -1;
	int curidx = -1;
	for( int i = strLen - 1; i > 0; i-- )
	{
		for( int j = i + 1; j < strLen; j++ )
		{
			if( str[i] == str[j] && table[i][j] )
			{
				table[i-1][j+1] = table[i][j];

				int len = j - i + 1;
				if( len > maxLen )
				{
					maxLen = len;
					curidx = i;
				}
			}
		}
	}

	
	std::string strObj(str);
	palindromicStr = strObj.substr( curidx, maxLen );
	DestructTable( strLen + 1, table );


	return maxLen;

}





/*
* Test interface
*
*/
void TestLongPalinromicSubstr()
{
	//const char* str = "abcdedcba";
	const char* str = "weewetttcfrdddgeeksskeegdddfcewwwssssrfffrrrrwwwr\
		               eaqwwwaacdddddoeoededleegdefgefefkegeeffsskkkmced";
	size_t strLen = strlen( str );


	std::string strObj;
	int res = LongPalindromicSubstrBrute( str, strObj );
	printf( "palindromic substr is %s by brute force method \n", strObj.c_str() );

	strObj.clear();
	res = LongPalindromicSubstrDP( str, strObj );
	printf( "palindromic substr is %s by dynamic programming \n", strObj.c_str() );


	
	strObj.clear();
	res = LongPalindromicSubstrDPExt( str, strObj );
	printf( "palindromic substr is %s by dynamic programming extended \n", strObj.c_str() );

}





#endif 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值