题解 | #密码截取#

密码截取

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

#include <iostream>
using namespace std;

int main() {
    string str;
    cin >> str;
    int odd_str = 1, even_str = 0;
    for(int i = 1; i < str.length() - 1; i++){//情况1:有效密码串长度为奇数
        int odd_cur_count = 1;//记录当前有效密码串长度
        int left = i - 1, right = i + 1;
        while(left >= 0 && right < str.length()){
            if(str[left] == str[right]){
                odd_cur_count += 2;
                left--;
                right++;
            }
            else break;
        }
        odd_str = max(odd_str, odd_cur_count);
    }

    for(int i = 0; i < str.length() - 1; i++){//情况2:有效密码串长度为偶数
        int even_cur_count = 0;
        int left = i, right = i + 1;
        while(left >= 0 && right < str.length()){
            if(str[left] == str[right]){
                even_cur_count += 2;
                left--;
                right++;
            }
            else break;
        }
        even_str = max(even_str, even_cur_count);
    }
    cout << max(odd_str, even_str) << endl;
    return 0;
}

全部评论

相关推荐

05-30 18:54
武汉商学院 Java
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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