题解 | #密码截取#

密码截取

https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1

#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 直接滑动窗口也不是不能做,就是可能没办法全部AC
// 从中间扩散开
int main() {
    string strs1;
    getline(cin,strs1);
    int maxLen = 1;
    for(int i = 1;i<strs1.size();i++)
    {
        int tempLen = 0;
        int k = 0;
        // 偶子串
        while(i-k>=0 && i+k+1 < strs1.size())
        {
            if(strs1[i-k] == strs1[i+k+1])
            {
            k++;
            tempLen += 2;
            }
            else{break;}
        }
        if(tempLen > maxLen)
        {
            maxLen = tempLen;
        }
        k = 1;
        tempLen = 1;
        while(i-k >= 0 && i+k < strs1.size())
        {
            if(strs1[i-k] == strs1[i+k])
            {
                k++;
                tempLen += 2;
            }
            else {break;}
            
        }
        if(tempLen > maxLen)
        {
            maxLen = tempLen;
        }
    }
    cout << maxLen << endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

ResourceUtilization:四六级不愧是大学最有用的证之一
点赞 评论 收藏
分享
雪飒:我也遇见过,我反问他有考虑来华为od吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务