题解 | #最长回文子串#

最长回文子串

https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507?tpId=37&tqId=21308&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26pageSize%3D50%26search%3D%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

#include <iostream>
using namespace std;

int main() {
    string str;
    getline (cin, str);
    int count1 = 0, count2 = 1, maxn = 0, pos = 1;
    for (int i = 0; i < str.length(); i++) {      //abcba型对称回文串的计算
        while (str[i - pos] == str[i + pos]) {
            count2 += 2;
            pos += 1;
            maxn = max(count2, maxn);
            if (str[i - pos] != str[i + pos]) {
                count2 =1;
                pos =1;
                break;
            }
        }
        while (str[i] == str[i + pos]) {      //abba型对称回文串的计算
            count1 += 2;
            pos += 2;
            maxn = max(count1, maxn);
            i -= 1;
            if (str[i] != str[i + pos] || i <0) {
                i += (count1 / 2 + 1);
                count1 = 0;
                pos = 1;
            }
        }
    }
    cout << maxn << endl;
}

全部评论

相关推荐

09-24 18:30
已编辑
长春工业大学 产品经理
小肥罗:HR就是好人的缩写哈哈哈哈
点赞 评论 收藏
分享
東大沒有派對:这是好事啊(峰哥脸
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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