名企笔试:PPTV2015研发工程师笔试题(对称子字符串转自微信公众号:算法爱好者

输出字符串中对称的子字符串的最大长度。

输入描述:

测试输入包含1个测试用例,一个字符串str。

输出描述:

输出最长的子字符串长度。

输入例子:

roorle

输出例子:

4

链接:https://mp.weixin.qq.com/s?__biz=MzI1MTIzMzI2MA==&mid=2650560782&idx=2&sn=e15ea73c2fcbf62f050b1ef9ae7d0a1f&chksm=f1feef8dc689669b232c09ab799d4c4972baf08858f112427b2d14cb5471e76aeed6164282ce&scene=0#rd



代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

int MaxLength( char* s )
{
    int ans = 1;
    int len = std::strlen(s);
    int Record[len];
    Record[0] = 1;
    for (int i = 1; i < len ; ++i)
    {
        int max = 1;
        if ( (i-Record[i-1]-1) >= 0 && s[i]==s[i-Record[i-1]-1])
        {
            max = std::max(max,( Record[i-1]+2));
        }
        int k = 1;
        while( s[i] == s[i-k] )
        {
            k++;
        }
        max = std::max( max, k );
        Record[i] = max;
        ans = std::max( max , ans );
    }
    return ans;
}

char s[100010];
int main()
{
    //std::cout << "Hello, World!" << std::endl;
    freopen("F:\\CSLeaning\\Thinking in C++\\MaxLengh\\in.in" , "r", stdin);
    memset(s, 0, sizeof(s));
    while( std::cin>>s )
    {
        std::cout<<MaxLength(s)<<std::endl;
        memset(s, 0, sizeof(s));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值