最长对称字符子串

问题: 输入一个字符串,输出该字符串中对称的子字符串的最大长度。比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4。
测试用例:
输入:iqiyiyiq
abccba
输出:4
3

int MaxLenSub(char *str)
{

    if (str==NULL|| *str=='\0' )
        return 0;

    int length=strlen(str);
    int new_len=1,len=1;    
    int cnt=0;
    int cnt1=0;
    int left=cnt-1,right=cnt+1;
    //奇数子串
    while(cnt<length-1)
    {
        len=1;
        left=cnt-1;right=cnt+1;
        while(left>=0 && right<=length-1 && *(str+left)==*(str+right))
        {
            len=right-left+1;
            left--;
            right++;
        }
        if(new_len<len)
            new_len=len;
        cnt++;
    }
    //偶数子串
    cnt=0;
    cnt1=1;
    while(cnt1<length-1)
    {
        if(*(str+cnt)==*(str+cnt1))
        {
            len=1;
            left=cnt-1;right=cnt1+1;
            while(left>=0 && right<=length-1 && *(str+left)==*(str+right))
            {
                len=right-left+1;
                left--;
                right++;
            }
        }
        if(new_len<len)
                new_len=len;
        cnt++;
        cnt1++;
    }

    cout<<new_len<<endl;
    return new_len/2+new_len%2;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值