字符串对称

题目:输入一个字符串,输出该字符串中对称的子字符串的最大长度。比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4。

#include<iostream>
#include
<string>
using namespace std;

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

int maxLength = 1;
char* pChar = pString;

char* Position;
int currentLength;

while(*pChar != '\0'){
char* pFirst = pChar-1;
char* pLast = pChar+1;
while(pFirst>=pString && *pLast != '\0' && *pFirst == *pLast){
--pFirst;
++pLast;
}

currentLength
= pLast-pFirst-1;
if(currentLength>maxLength){
maxLength
= currentLength;
Position
= pChar;
}

pFirst
= pChar;
pLast
= pChar+1;
while(pFirst>=pString && *pLast != '\0' && *pFirst == *pLast){
--pFirst;
++pLast;
}

currentLength
= pLast-pFirst-1;
if(currentLength>maxLength){
maxLength
= currentLength;
Position
= pChar;
}

pChar
++;
}
return maxLength;
}

int main(){
char* pString = "ab";
cout
<< "The length of pString: " << strlen(pString) << endl;

cout
<< "The Longest symmetry of pString is: " << MaxLength(pString) << endl;

return 0;
}

转载于:https://www.cnblogs.com/phoenixzq/archive/2011/08/05/2129018.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值