C/C++编程题之找出2个给定字符串中最长的公共子字符串

/******************************************************************************************************
Description     找出2个给定字符串中最长的公共子字符串
Prototype       void vProcTaskToCoreByLayerAndTime(unsigned int Layer,unsigned int* pDuration)
Input Param     char* pSrcStr1, char* pSrcStr2: 给定的2个字符串
Output Param    char* pSubStr : 从给定的字符串中找到的最长公共子串
Return Value    
********************************************************************************************************/
#include 
#include 

char Small2Big(char ch)
{
	if(ch >= 'a' && ch <= 'z')
		return (ch-32);
	return ch;
}

void vGetTheLongestSubStringInTwoStrings(char* pSrcStr1, char* pSrcStr2, char* pSubStr)
{
	if(pSrcStr1 == NULL || pSrcStr2 == NULL || pSubStr == NULL)
		return;
	char word[1024];
	char tmpword[1024];
	memset(word,0x00,sizeof(word));
	memset(tmpword,0x00,sizeof(tmpword));

	int num = 0,tmp = 0,tmpStr1 = 0,flag = 0;
	unsigned int len1 = (unsigned int)strlen(pSrcStr1);
	unsigned int len2 = (unsigned int)strlen(pSrcStr2);
	for(unsigned int m = 0; m < len1; m++)
	{
		tmpStr1 = m;
		tmp = 0;
		flag = 0;
		memset(tmpword,0x00,sizeof(tmpword));
		for(unsigned int i = 0; i < len2; )
		{

			char trans1 = Small2Big(pSrcStr1[m]);
			char trans2 = Small2Big(pSrcStr2[i]);
			if(trans1 == trans2)
			{
				flag = 1;
				tmpword[tmp++] = pSrcStr1[m];	
				m++;
				i++;
				continue;
			}
			tmpword[tmp] = '\0';
			if( flag == 1 && tmp >= num)
			{
				strcpy_s(word,tmp+1,tmpword);
				memset(tmpword,0x00,sizeof(tmpword));
				num = tmp;
				tmp = 0;
				flag = 0;
			}
			else
			{
				memset(tmpword,0x00,sizeof(tmpword));
				tmp = 0;
				flag = 0;
				m = tmpStr1;
			}
			i++;
		}
		tmpword[tmp] = '\0';
		if( flag == 1 && tmp >= num)
		{
			strcpy_s(word,tmp+1,tmpword);
			memset(tmpword,0x00,sizeof(tmpword));
			num = tmp;
			tmp = 0;
			flag = 0;
		}

		m = tmpStr1;
	}
	strcpy_s(pSubStr,num+1,word);
}


 
 

你可能感兴趣的:(c语言,字符串,公共字串)