题解 | 密码截取

密码截取

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

#include <iostream>
#include <string>

using namespace std;

// 检查字符串是否是回文
bool isPalindrome(const string &s) {
    int start = 0;
    int end = s.size() - 1;
    while (start < end) {
        if (s[start] != s[end]) {
            return false;
        }
        start++;
        end--;
    }
    return true;
}

int main() {
    string s;
    cin >> s;

    int n = s.length();
    int maxLength = 0;

    // 检查每个可能的子串
    for (int i = 0; i < n; ++i) {
        for (int j = i; j < n; ++j) {
            string substring = s.substr(i, j - i + 1);
            if (isPalindrome(substring)) {
                maxLength = max(maxLength, j - i + 1);
            }
        }
    }

    cout << maxLength << endl;
    return 0;
}

#牛客春招刷题训练营#
全部评论

相关推荐

LZStarV:冲就好了,就算真的是字节也冲,面评脏了大不了等三四个月就淡了,而且等到那个时候实力进步了选择还多,何必拘泥于字节
点赞 评论 收藏
分享
09-14 17:23
门头沟学院
故事和酒66:所以说副业很重要,程序员干到40岁,再怎么也赚300万了,吃吃利息也够活下去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务