题解 | #密码截取#

密码截取

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

#include <iostream>
using namespace std;

int palindrome(const string & str, int x, int y)
{
    int leng = str.length();
    while (x >= 0 && y < leng && str.at(x) == str.at(y)) {
        x--;
        y++;
    }
    return y - x - 1;
}


int main() {
    string strInput;
    while (cin >> strInput) {
        int max = 0, leng = strInput.length();
        for (int i = 0; i < leng; i++) {
            int pal1 = palindrome(strInput, i, i);
            int pal2 = palindrome(strInput, i, i + 1);
            if (pal1 > max) {
                max = pal1;
            }
            if (pal2 > max) {
                max = pal2;
            }
        }
        cout << max << endl;
    }
}

全部评论

相关推荐

团子请爱我一次_十月...:不是戈门,干哪来了,这就是java嘛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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