题解 | #密码截取#

密码截取

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

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int ans = 0, i = 0 , l , r ,len;
    vector<char> str;
    char a;
    while(a != '\n'){
        a = getchar();
        if (a == '\n'){
            str.pop_back();
            break;
        }
        str.push_back(a);
        str.push_back('#');
    }
    for(i = 0 ; i < str.size() ; i++){
        l = i - 1;
        r = i + 1;
        len = (str[i] == '#')? 0 : 1;
        while(l >= 0 && r < str.size() && str[l] == str[r]){
            len += (str[l] == '#')? 0 : 2;
            l--;
            r++;
        }
        ans = (ans < len)? len : ans;
    }
    cout<<ans;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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