046-字符串

 
// 046.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "string.h"

//判断字符串是否对称(起始地址终止地址)
bool IsSymmetrical(char *pBegin, char *pEnd)
{
	if (NULL == pBegin || NULL == pEnd || pBegin > pEnd)
	{
		return false;
	}

	while(pBegin < pEnd)
	{
		if (*pBegin != *pEnd)
		{
			return false;
		}

		pBegin++;
		pEnd--;
	}
	return true;
}


int GetLongestSymmetricalLength_1(char *pString)
{
	if (NULL == pString)
	{
		return 0;
	}

	//思路
	//pFirst依次指向每一个字符串,除最后一个字符串
	//pFirst指定一个字符串后,pLast首先指向pFirst后一个字符串,
	//然后依次遍历至pFirst后整个字符串查找是否有对称的字符
	int symmetricalLen = 1;
	char *pFirst = pString;
	int len = strlen(pString);
	while(pFirst < &pString[len-1])
	{
		char *pLast = pFirst+1;

		while(pLast <= &pString[len-1])
		{
			if (IsSymmetrical(pFirst, pLast))
			{
				int newLen = pLast - pFirst + 1;
				if (newLen > symmetricalLen)
				{
					symmetricalLen = newLen;
				}
			}
			pLast++;
		}
		pFirst++;
	}

	return symmetricalLen;

}

//题目:输入一个字符串,输出该字符串中对称的子字符串的最大长度。比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4。
int main(int argc, char* argv[])
{
	char pBuffer[7] = "google";
	bool bRet = IsSymmetrical(pBuffer, &pBuffer[strlen(pBuffer)-1]);
	int ret =  GetLongestSymmetricalLength_1(pBuffer); 
	return 0;
}


你可能感兴趣的:(Google,null)